﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Text.RegularExpressions;
using DAL;
using Entities;
using Entities.IRepository;

namespace QuanLyBilliards_Coffee
{
    public partial class Form1 : Form
    {
        public static string tennguoidangnhap="";
        HangHoaRepository hanghoa = new HangHoaRepository();
        BanRepository dsban = new BanRepository();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            LoadFrom();
            
            
        }

        private void LoadFrom()
        {
            tabQuanLy.Visible = false;
            lblChaoMung.Visible = true;
            lblUser.Visible = false;
            lblPass.Visible = false;
            txtUserName.Visible = false;
            txtPassWord.Visible = false;
            btnOKThemTK.Visible = false;
            cbbThongKe.SelectedIndex = 0;
            dtpThongKeTheoNgay.Visible = true;
            cbbTimKiemHoaDon.SelectedIndex = 0;
            //cbbKieuTimKiemTaiKhoan.SelectedIndex = 0;
            cbbKieuTimKiemHangHoa.SelectedIndex = 0;
            lblTimKiemHangHoa.Visible = false;
            dtpTimKiemHangHoa.Visible = false;
            txtTimKiemHangHoa.Visible = false;
            cbbKieuTimKiemHangHoa.Visible = true;
            cbbTimKiemHangHoa.Visible = false;
           // cbbKieuTimKiemKhachHang.SelectedIndex = 0;
            lblThongbao.Visible = false;
            timer1.Start();
            timer2.Start();
            rbThongKeDoanhThu.Checked = true;
            ////ITaiKhoanRepository tk = new TaiKhoanRepository();
            //LoadLVDSTaiKhoan(tk.GetAll());
            //TaiKhoanRepository thk = new TaiKhoanRepository();
            //LoadLVDSTaiKhoan(thk.GetAll());
           // tabQLBan.PerformLayout();
            //////////////////////
            //Load bàn cafe,billards
            BanRepository ban = new BanRepository();
            LoadBanCF(ban.GetAllCafe());
            LoadBanBL(ban.GettAllBillards());
            cbbTimKiemHoaDon.SelectedIndex = 0;
            HoaDonRepository hd = new HoaDonRepository();
            LoadLVDanhSachHoaDon(hd.GetAll());

            LoadListView(hanghoa.GetAll());
            //////
            LoadQLBan();
            txtTenNhanVienLap.Text = tennguoidangnhap;


        }

        private void tabQuanLy_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabQuanLy.SelectedTab == tabQuanLy.TabPages["tabQLKhachHang"])
            {


                LoadListView(khs.GetAll().ToList());

            }
            if (tabQuanLy.SelectedTab == tabQuanLy.TabPages["tabQLNhaCungCap"])
            {
                LoadLV(nhc.GetAll().ToList());
            }
            if (tabQuanLy.SelectedTab == tabQuanLy.TabPages["tabQLTaiKhoan"])
            {
                LoadLVDSTaiKhoan(tkh.GetAll().ToList());
            }
        }

        // LOGIN
#region
        public bool KiemTraDangNhap(ref string s)
        {
            s = "";
            if (XuLyChuoiVao.CatKhoangTrangDu(txtuser.Text) == "")
                s += "Bạn chưa nhập tên đăng nhập";
            if(XuLyChuoiVao.CatKhoangTrangDu(txtPass.Text)=="")
                s+="\nBạn chưa nhập pass";
            if (s == "")
                return true;
            return false;
        }
        private void btnLogin_Click(object sender, EventArgs e)
        {
            string s = "";
            if (!KiemTraDangNhap(ref s))
            {
                MessageBox.Show(s);
            }
            else
            {
                string ten = txtuser.Text;
                string pass = txtPass.Text;
                ITaiKhoanRepository tk = new TaiKhoanRepository();
                TaiKhoan tkdn = tk.GetByTenUser(XuLyChuoiVao.CatKhoangTrangDu(ten));
                if (tk.KiemTraDangNhap(XuLyChuoiVao.CatKhoangTrangDu(ten), pass))
                {
            btnCancleLogin.Visible = false;
            lblChaoMung.Visible = false;
            txtPass.Text = "";
            txtuser.Text = "";
            btnLogin.Visible = false;
            lblUserDangNhap.Visible = false;
            lblPassDangNhap.Visible = false;
            txtuser.Visible = false;
            txtPass.Visible = false;
            lblPQ.Visible = false;
            lblTenUser.Visible = false;
            txtPQ.Visible = false;
            txtTenUser.Visible = false;
            lblThongbao.Visible = true;
                    string thongbao = "Xin chào ";
                    thongbao += tkdn.TenUser;
                    lblThongbao.Text = thongbao;
            tennguoidangnhap = tkdn.TenUser;
            txtTenNhanVienLap.Text = tennguoidangnhap;
                    //if(tkdn.PhanQuyen==2.)
                    //.Controls.
                    tabQuanLy.Visible = true;
                }
                else
                {
                    MessageBox.Show("bạn đăng nhập sai tên hoặc mật khẩu");
                }
            }
        }
       
        private void btnCancleLogin_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion
        // TÀI KHOẢN
#region
        public enum KieuTimTK
        {
            UserName,
            TenUser
        }
        public KieuTimTK kieutimtk;
        public ITaiKhoanRepository tkh = new TaiKhoanRepository();
        public TaiKhoanRepository th = new TaiKhoanRepository();
        public TaiKhoan tkdn = new TaiKhoan();
        private void btnTimKiemTaiKhoan_Click(object sender, EventArgs e)
        {
            string data = txtTimKiemTaiKhoan.Text;
            List<TaiKhoan> ds = new List<TaiKhoan>();
            switch (kieutimtk)
            {
                case KieuTimTK.UserName:
                    ds = tkh.TimTheoTenTaiKhoan(data);
                    if (ds.Count > 0)
                        LoadLVDSTaiKhoan(ds);
                    else
                        MessageBox.Show("Không tìm thấy tên người dùng này");
                    break;
                case KieuTimTK.TenUser:
                    ds = tkh.TimTheoTenNguoiDN(data);
                    if (ds.Count > 0)
                        LoadLVDSTaiKhoan(ds);
                    else
                        MessageBox.Show(" Không tìm thấy User này");
                    break;
                default:
                    break;
            }
        }
        private void rdTenDN_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton rdb = sender as RadioButton;
            switch (rdb.Name)
            
            {
                case "rdTenDN":
                    lblTTTK.Text = "Tìm Theo Tên Tài Khoản";
                    kieutimtk = KieuTimTK.UserName;
                    break;
                case "rdTenUser":
                    lblTTTK.Text = "Tìm Theo Tên Người Đăng Nhập";
                    kieutimtk = KieuTimTK.TenUser;
                    break;
                default:
                    break;
            }
        }

        private void LoadLVDSTaiKhoan(List<TaiKhoan> list)
        {
            lvDSTaiKhoan.Items.Clear();
            foreach (var item in list)
            {
                ListViewItem it = new ListViewItem(item.UserName);
                it.SubItems.Add(item.Password);
                ////it.SubItems.Add(item.UserName);
                it.SubItems.Add(item.TenUser);
                it.SubItems.Add(item.PhanQuyen.ToString());
                lvDSTaiKhoan.Items.Add(it);
            }
        }
        public static bool KiemTraKhoangTrang(string s)
        {
            string pattern = @"\s{1,}";
            Regex rg = new Regex(pattern, RegexOptions.IgnoreCase);
            if (!rg.IsMatch(s))
            {

                return false;
            }
            return true;
        }
        private void btnOKThemTK_Click_1(object sender, EventArgs e)
        {
            string s = txtUserName.Text; ;
            string s2 = txtPassWord.Text; ;
            TaiKhoan tks = getControl();
            if (txtUserName.Text.Trim().Length != 0 || txtPassWord.Text.Trim().Length != 0)
            {                
                if (tkh.AddNew(tks))
                {
                    MessageBox.Show("Thêm thành công");
                    LoadLVDSTaiKhoan(tkh.GetAll().ToList());
                }
                else
                    MessageBox.Show("Thất bại");
            }
            else
                MessageBox.Show(" UserName và Password bắt buộc không chứa khoảng trắng");
        }
        //private void btnOKThemTK_Click(object sender, EventArgs e)
        //{
        //    string s="";
        //    string s2="";
        //    TaiKhoan tks = getControl();
        //    if (!KiemTraKhoangTrang(s) == false && !KiemTraKhoangTrang(s2) == false)
        //    {
        //        if (tkh.AddNew(tks))
        //        {
        //            MessageBox.Show("Thêm thành công");
        //        }
        //        else
        //            MessageBox.Show("Thất bại");
        //    }
        //    else
        //        MessageBox.Show(" UserName và Password bắt buộc không chứa khoảng trắng");
        //}
        private void btnThemTaiKhoan_Click_1(object sender, EventArgs e)
        {
            lblUser.Visible = true;
            lblPass.Visible = true;
            lblTenUser.Visible = true;
            lblPQ.Visible = true;
            txtUserName.Visible = true;
            txtPassWord.Visible = true;
            txtTenUser.Visible = true;
            txtPQ.Visible = true;
            btnOKThemTK.Visible = true;
        }
        private void xoaTaiKhoan_Click(object sender, EventArgs e)
        {
            if (lvDSTaiKhoan.SelectedItems.Count > 0)
            {
                string us = lvDSTaiKhoan.SelectedItems[0].SubItems[0].Text;
                ITaiKhoanRepository tkk = new TaiKhoanRepository();
                TaiKhoan tk = tkk.GetByTenUser(us);
                tkk.Delete(tk);
                MessageBox.Show("Xoa thanh cong");
                LoadLVDSTaiKhoan(tkk.GetAll().ToList());

            }
            else
            {
                MessageBox.Show("Ban chua chon tai khoan can xoa");
            }
        }
        private TaiKhoan getControl()
        {
            TaiKhoan t = new TaiKhoan();
            t.TenUser = txtTenUser.Text;
            t.UserName = txtuser.Text;
            t.Password = txtPassWord.Text;
            t.PhanQuyen = 2;
            return t;
        }
#endregion
        // NHÀ CUNG CẤP
#region
        public enum KieuTimNCC
        {
            SĐT,
            TenNhaCC,
            HHCungCap
        }
        public KieuTimNCC kieu;
        public INhaCungCapRepository nhc = new NhaCungCapRepository();
        public static string mancc = null;
        public static string tenncc;
        public static bool state = false;
        NhaCungCap ncc = new NhaCungCap();
        private void LoadLV(List<NhaCungCap> list)
        {
            string kq = "";
            lvNhaCungCap.Items.Clear();
            NhaCungCap ct;
            foreach (NhaCungCap ncc in list)
            {
                kq = "";
                ListViewItem item = new ListViewItem(ncc.MaNCC);
                item.SubItems.Add(ncc.TenNCC);
                ct = new NhaCungCap();
                ct = nhc.getByIdDetail(ncc.MaNCC);

                if (ct.dshanghoa.Count > 0)
                {
                    kq += ct.TenDSHH;
                }
                else
                    kq += "Chưa Có Hàng Hóa Nào";


                item.SubItems.Add(kq);
                item.SubItems.Add(ncc.SDT);
                item.SubItems.Add(ncc.DiaChi);
                //item.SubItems.Add(ncc.DiaChi);
                lvNhaCungCap.Items.Add(item);
            }
        }
        private void btnThemNcC_Click(object sender, EventArgs e)
        {

            FormThemNCC frm = new FormThemNCC();
            frm.FormClosed +=new FormClosedEventHandler(frm_FormClosed);
            frm.ShowDialog();
        }

        void fm_FormClosed(object sender, FormClosedEventArgs e)
        {
            LoadLV(nhc.GetAll().ToList());
        }
        private void lvNhaCungCap_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.lvNhaCungCap.SelectedItems.Count > 0)
            {
                mancc = this.lvNhaCungCap.SelectedItems[0].SubItems[0].Text;

            }
        }
        private void btnTimNCC_Click(object sender, EventArgs e)
        {
            string data = txtThongTinTimKiemNCC.Text;
            List<NhaCungCap> ds = new List<NhaCungCap>();
            switch (kieu)
            {
                case KieuTimNCC.SĐT:
                    ds = nhc.TimTheoSoDT(data);
                    if (ds.Count > 0)
                        LoadLV(ds);
                    else MessageBox.Show("Không tìm thấy");

                    break;
                case KieuTimNCC.TenNhaCC:
                    ds = nhc.TimTheoTenNCC(data);
                    if (ds.Count > 0)
                        LoadLV(ds);
                    else MessageBox.Show("Không tìm thấy");
                    break;
                case KieuTimNCC.HHCungCap:
                    {
                        lvNhaCungCap.Items.Clear();
                        IHangHoaRepository hh = new HangHoaRepository();
                        IQueryable<HangHoa> listhh = hh.TimHangHoaTheoTen(data);
                        foreach (var item in listhh)
                        {
                            NhaCungCap ncc = nhc.GetByMaNhaCungCap(item.NhaCC);
                            if (ncc != null)
                                ds.Add(ncc);
                        }
                        //ds = nhc.TimTheoHHCC(data);
                    }
                    if (ds.Count > 0)
                    {
                        foreach (var item in ds)
                        {
                            string kq = "";
                            ListViewItem it = new ListViewItem(item.MaNCC);
                            it.SubItems.Add(item.TenNCC);
                            NhaCungCap ct = new NhaCungCap();
                            ct = nhc.getByIdDetail(item.MaNCC);

                            if (ct.dshanghoa.Count > 0)
                            {
                                kq += ct.TenDSHH;
                            }
                            else
                                kq += "Chưa Có Hàng Hóa Nào";

                            it.SubItems.Add(kq);
                            it.SubItems.Add(item.SDT);
                            it.SubItems.Add(item.DiaChi);
                            //item.SubItems.Add(ncc.DiaChi);
                            lvNhaCungCap.Items.Add(it);
                        }
                        //LoadLV(ds);
                    }
                    else
                        MessageBox.Show(" Không tìm thấy");
                    break;
                default:
                    break;
            }
        }
        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton rd = sender as RadioButton;
            switch (rd.Name)
            {
                case "rdSdt":
                    lbdl.Text = "Tìm Theo Số ĐT";
                    kieu = KieuTimNCC.SĐT;
                    break;
                case "radtenhanghoacung":
                    lbdl.Text = "Tìm Theo Hàng Hóa CC";
                    kieu = KieuTimNCC.HHCungCap;
                    break;
                case "radtenncc":
                    lbdl.Text = "Tìm Theo Tên NCC";
                    kieu = KieuTimNCC.TenNhaCC;
                    break;
                default:
                    break;
            }
        }
        private void btnload_Click(object sender, EventArgs e)
        {
            LoadLV(nhc.GetAll().ToList());
        }
        private void btnXoaNCC_Click(object sender, EventArgs e)
        {
            //DialogResult rs = new DialogResult();
            if (lvNhaCungCap.SelectedItems.Count > 0)
            {
                String ma = lvNhaCungCap.SelectedItems[0].SubItems[0].Text;
                NhaCungCap nhcc = nhc.getByIdDetail(ma);
                //bool xoa = nhc.Delete(nhcc);
                bool xoa = nhc.Delete(nhcc);
                if (xoa)
                {
                    MessageBox.Show("Xóa Thành Công");
                    LoadLV(nhc.GetAll().ToList());
                }

                else
                {
                    MessageBox.Show("Thất Bại ");
                }

            }
        }
        private void btnCapNhapNCC_Click(object sender, EventArgs e)
        {
            state = true;
            int i = lvNhaCungCap.SelectedItems.Count - 1;
            mancc = lvNhaCungCap.SelectedItems[i].SubItems[0].Text;
            FormThemNCC fr = new FormThemNCC();
            fr.FormClosed += new FormClosedEventHandler(frm_FormClosed);
            fr.ShowDialog();

        }
        //private void txtThongTinTimKiemNCC_Validating(object sender, CancelEventArgs e)
        //{
        //    if (rdSdt.Checked == false && rdHHCC.Checked == false && rdTenNCC.Checked == false)
        //    {
        //        this.Error.SetError(txtThongTinTimKiemNCC, " Bạn phải chọn chức năng tìm kiếm");
        //        e.Cancel = false;
        //    }
        //}

        //private void txtThongTinTimKiemNCC_Validated(object sender, EventArgs e)
        //{
        //    this.Error.SetError(txtThongTinTimKiemNCC, "");
        //}

        #endregion


        // BÀN


        // các xử lý trên tab QL Bàn
        #region

        private void btnbatDauTinhGio_Click(object sender, EventArgs e)
        {
            timer1.Stop();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            lblGioBatDau.Text = DateTime.Now.ToLongTimeString();
            
        }
        private void timer2_Tick(object sender, EventArgs e)
        {
            lblGioKetThuc.Text = DateTime.Now.ToLongTimeString();
        }

        private void btnKetThucTinhGio_Click(object sender, EventArgs e)
        {
            timer2.Stop();

        }

        private void btnThemBanCafe_Click(object sender, EventArgs e)
        {
            FormThemBanCafe f = new FormThemBanCafe();
            f.ShowDialog();
            LoadBanCF(dsban.GetAllCafe());
        }

        public void LoadBanCF(IQueryable<Ban> list)
        {
            lvBanCaFe.Items.Clear();
            foreach (Ban b in list)
            {
                ListViewItem item = lvBanCaFe.Items.Add(b.TenBan);
                if (b.TrangThai == false)
                {
                    item.SubItems.Add("Đang rảnh");
                }
                else
                    item.SubItems.Add("đang bận");
            }
        }
        public void LoadBanBL(IQueryable<Ban> list)
        {
            lvBanBillrrds.Items.Clear();
            foreach (Ban b in list)
            {
                ListViewItem item = lvBanBillrrds.Items.Add(b.TenBan);
                if (b.TrangThai == false)
                {
                    item.SubItems.Add("Đang rảnh");
                }
                else
                {
                    item.SubItems.Add("đang bận");
                }
                item.SubItems.Add(b.Gia.ToString());
            }
        }
        private void btnXoaBanCF_Click(object sender, EventArgs e)
        {
            if (lvBanCaFe.SelectedItems.Count > 0)
            {
                String ma = lvBanCaFe.SelectedItems[0].SubItems[0].Text;
                BanRepository ban = new BanRepository();
                Ban a = ban.GetByTenBan(ma);
                HoaDonRepository hoadon = new HoaDonRepository();
                IQueryable<HoaDon> listhd = hoadon.TimHoaDonTheoBan(ma);
                ChiTietHoaDonRepository chitiethd = new ChiTietHoaDonRepository();
                if (listhd == null)
                {
                    //MessageBox.Show("vgfhfgj");
                    return;
                }
                else
                {
                    foreach (HoaDon hda in listhd)
                    {
                        IQueryable<ChiTietHoaDon> listcthd = chitiethd.getByMaHD(hda.MaHD);
                        foreach (ChiTietHoaDon cthda in listcthd)
                        {
                            chitiethd.Delete(cthda);
                            //chitiethd.DeleteByMaHD(hda.MaHD);
                        }
                        hoadon.Delete(hda);
                        break;
                    }
                    if (ban.Delete(a))
                    {
                        MessageBox.Show("Xóa thành công");
                        LoadBanCF(ban.GetAllCafe());
                    }
                    else
                    {
                        MessageBox.Show("Lỗi");
                    }
                }
            }
            else
            {
                MessageBox.Show("Bạn chưa chọn DL muốn xóa");
            }

        }

        private void btnXoabanBL_Click(object sender, EventArgs e)
        {
            if (lvBanBillrrds.SelectedItems.Count > 0)
            {
                String ma = lvBanBillrrds.SelectedItems[0].SubItems[0].Text;
                BanRepository ban = new BanRepository();
                Ban a = ban.GetByTenBan(ma);
                if (ban.Delete(a))
                {
                    MessageBox.Show("Xóa thành công");
                    LoadBanBL(ban.GettAllBillards());
                }
                else
                {
                    MessageBox.Show("Lỗi");
                }
            }
            else
            {
                MessageBox.Show("Bạn chưa chọn DL muốn xóa");
            }
        }

        private void btnTimKiemBanCF_Click_1(object sender, EventArgs e)
        {
            FormTimKiemBan f = new FormTimKiemBan();
            f.ShowDialog();
        }


        private void btnTimKiemBanBiza_Click(object sender, EventArgs e)
        {
            FormTimKiemBan f = new FormTimKiemBan();
            f.ShowDialog();

        }
        //private void lvBanCaFe_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    if (lvBanCaFe.SelectedItems.Count > 0)
        //    {
        //        MessageBox.Show(lvBanCaFe.SelectedItems[0].SubItems[0].Text);
        //    }
        //}


        private void lvBanBillrrds_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvBanBillrrds.SelectedItems.Count > 0)
            {
                MessageBox.Show(lvBanBillrrds.SelectedItems[0].SubItems[0].Text);
            }
        }

        public static int hamkt = 0;
        public static string maban = null;
        private void btnCapNhatBanCF_Click(object sender, EventArgs e)
        {
            hamkt = 1;
            if (lvBanCaFe.SelectedItems.Count > 0)
            {
                FormCapNhatban f = new FormCapNhatban();
                maban = lvBanCaFe.SelectedItems[0].SubItems[0].Text;
                f.ShowDialog();
                LoadBanCF(dsban.GetAllCafe());
            }
            else
            {
                MessageBox.Show("bạn chưa chọn bàn để cập nhật");
            }

        }

        private void btnCapNhatBanBL_Click(object sender, EventArgs e)
        {
            hamkt = 2;
            if (lvBanBillrrds.SelectedItems.Count > 0)
            {
                FormCapNhatban f = new FormCapNhatban();
                maban = lvBanBillrrds.SelectedItems[0].SubItems[0].Text;
                f.ShowDialog();
                LoadBanBL(dsban.GettAllBillards());
            }
            else
            {
                MessageBox.Show("bạn chưa chọn bàn để cập nhật");
            }
        }

        public void LoadQLBan()
        {
            txtTenNhanVien.Text = tennguoidangnhap;
            txtTenNhanVienLap.Text = tennguoidangnhap;
            cbbsluongthem.SelectedIndex = 0;
            LoadMatHang();
            LoadMatHang1();
        }
        public void LoadMatHang()
        {
            lvDanhSachMatHang.Items.Clear();
            IHangHoaRepository hh = new HangHoaRepository();
            IQueryable<HangHoa> listquachebien = hh.TimHangHoaTheoLoai("Qua chế biến");
            IQueryable<HangHoa> lisstkhongchebien = hh.TimHangHoaTheoLoai("không chế biến");
            foreach (HangHoa item in listquachebien)
            {
                ListViewItem lvit = lvDanhSachMatHang.Items.Add(item.TenHH);
                lvit.SubItems.Add(item.DonViTinh);
                lvit.SubItems.Add(item.GiaBan.ToString());
            }
            foreach (HangHoa item in lisstkhongchebien)
            {
                ListViewItem lvit = lvDanhSachMatHang.Items.Add(item.TenHH);
                lvit.SubItems.Add(item.DonViTinh);
                lvit.SubItems.Add(item.GiaBan.ToString());
            }
        }
        public void LoadMatHang1()
        {
            lvDanhSachMH.Items.Clear();
            IHangHoaRepository hh = new HangHoaRepository();
            IQueryable<HangHoa> listquachebien = hh.TimHangHoaTheoLoai("Qua chế biến");
            IQueryable<HangHoa> lisstkhongchebien = hh.TimHangHoaTheoLoai("không chế biến");
            foreach (HangHoa item in listquachebien)
            {
                ListViewItem lvit1 = new ListViewItem(item.TenHH);
                lvit1.SubItems.Add(item.DonViTinh);
                lvit1.SubItems.Add(item.GiaBan.ToString());
                lvDanhSachMH.Items.Add(lvit1);
            }
            foreach (HangHoa item in lisstkhongchebien)
            {
                ListViewItem lvit1 = new ListViewItem(item.TenHH);
                lvit1.SubItems.Add(item.DonViTinh);
                lvit1.SubItems.Add(item.GiaBan.ToString());
                lvDanhSachMH.Items.Add(lvit1);
            }
        }

        private void lvDanhSachMatHang_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbbsluongthem.SelectedIndex = 0;
        }

        public bool KiemTraMatHangDaCo(string s)
        {
            for (int i = 0; i < lvDanhSachCacMonDaGoi.Items.Count; i++)
                if (lvDanhSachCacMonDaGoi.Items[i].SubItems[0].Text == s)
                {
                    return true;
                }
            return false;
        }
        private void lvDanhSachCacMonDaGoi_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbbsluongthem.SelectedIndex = 0;
        }
        private void btnThemMatHangVaoHoaDon_Click(object sender, EventArgs e)
        {
            HangHoaRepository hanghoa = new HangHoaRepository();
            int i = lvDanhSachMatHang.SelectedItems.Count - 1;
            if (i >= 0)
            {
                if (!KiemTraMatHangDaCo(lvDanhSachMatHang.SelectedItems[i].SubItems[0].Text))
                {
                    HangHoa hh = hanghoa.TimTheoTen(lvDanhSachMatHang.SelectedItems[i].SubItems[0].Text);
                    ListViewItem lv = new ListViewItem(lvDanhSachMatHang.SelectedItems[i].SubItems[0].Text);
                    lv.SubItems.Add(cbbsluongthem.Text);
                    int tien = int.Parse(cbbsluongthem.Text) * hh.GiaBan;
                    lv.SubItems.Add(tien.ToString());
                    lvDanhSachCacMonDaGoi.Items.Add(lv);


                    i--;
                }
                else
                {
                    for (int j = 0; j < lvDanhSachCacMonDaGoi.Items.Count; j++)
                        if (lvDanhSachCacMonDaGoi.Items[j].SubItems[0].Text == lvDanhSachMatHang.SelectedItems[i].SubItems[0].Text)
                        {
                            int sl = int.Parse(lvDanhSachCacMonDaGoi.Items[j].SubItems[1].Text) + int.Parse(cbbsluongthem.Text);
                            lvDanhSachCacMonDaGoi.Items[j].SubItems[1].Text = sl.ToString();

                            HangHoa hh = hanghoa.TimTheoTen(lvDanhSachMatHang.SelectedItems[i].SubItems[0].Text);
                            int tien = int.Parse(lvDanhSachCacMonDaGoi.Items[j].SubItems[1].Text) * hh.GiaBan;
                            lvDanhSachCacMonDaGoi.Items[j].SubItems[2].Text = tien.ToString();


                        }
                }

            }
            TinhTienTheoBan();
            TinhTienThanhToan();


        }
        private void btnGiamMatHang_Click(object sender, EventArgs e)
        {
            int i = lvDanhSachCacMonDaGoi.SelectedItems.Count - 1;
            if (i >= 0)
            {
                if (lvDanhSachCacMonDaGoi.SelectedItems[i].SubItems[1].Text == "1")
                {
                    for (int j = 0; j < lvDanhSachCacMonDaGoi.Items.Count; j++)
                        if (lvDanhSachCacMonDaGoi.Items[j].SubItems[0].Text == lvDanhSachCacMonDaGoi.SelectedItems[i].SubItems[0].Text)
                            lvDanhSachCacMonDaGoi.Items.RemoveAt(j);

                }
                else
                {
                    for (int j = 0; j < lvDanhSachCacMonDaGoi.Items.Count; j++)
                    {
                        if (lvDanhSachCacMonDaGoi.Items[j].SubItems[0].Text == lvDanhSachCacMonDaGoi.SelectedItems[i].SubItems[0].Text)
                        {

                            if (int.Parse(cbbsluongthem.Text) - int.Parse(lvDanhSachCacMonDaGoi.Items[j].SubItems[1].Text) < 0)
                            {

                                int sl = int.Parse(lvDanhSachCacMonDaGoi.Items[j].SubItems[1].Text) - int.Parse(cbbsluongthem.Text);

                                lvDanhSachCacMonDaGoi.SelectedItems[i].SubItems[1].Text = sl.ToString();

                                HangHoa hh = hanghoa.TimTheoTen(lvDanhSachCacMonDaGoi.SelectedItems[i].SubItems[0].Text);
                                int tien = int.Parse(sl.ToString()) * hh.GiaBan;
                                lvDanhSachCacMonDaGoi.SelectedItems[i].SubItems[2].Text = tien.ToString();


                            }
                            else
                                if (int.Parse(cbbsluongthem.Text) - int.Parse(lvDanhSachCacMonDaGoi.SelectedItems[i].SubItems[1].Text) == 0)
                                {
                                    lvDanhSachCacMonDaGoi.Items.RemoveAt(j);

                                    return;
                                }
                                else
                                    MessageBox.Show("so luong giam vuot qua so luong da co");
                        }
                    }
                }
            }
            TinhTienTheoBan();
            TinhTienThanhToan();
        }
        public void TinhTienTheoBan()
        {
            int tongtien = 0;
            if (lvDanhSachCacMonDaGoi.Items.Count == 0)
            {
            }
            else
            {


                for (int i = 0; i < lvDanhSachCacMonDaGoi.Items.Count; i++)
                {
                    tongtien += int.Parse(lvDanhSachCacMonDaGoi.Items[i].SubItems[2].Text);
                }
            }
            txtThanhTien.Text = tongtien.ToString();

        }
        public void TinhTienThanhToan()
        {
            float tongtien = 0;
            float tiengiangia = 0;
            int tien = 0;
            if (txtGiamGia.Text == "")
            {
                txtTongTien.Text = txtThanhTien.Text;
            }
            else
            {
                int giamgia = int.Parse(txtGiamGia.Text);
                tien = int.Parse(txtThanhTien.Text);
                tiengiangia = tien * giamgia / 100;
                tongtien = tien - tiengiangia;
                txtTongTien.Text = tongtien.ToString();
            }
        }


        //private void lvBanCaFe_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    if (lvBanCaFe.SelectedItems.Count > 0)
        //    {
        //        string tenban = "";
        //        int i = lvBanCaFe.SelectedItems.Count - 1;

        //        lvDanhSachCacMonDaGoi.Items.Clear();
        //        IHoaDonRepository hd = new HoaDonRepository();
        //        IQueryable<HoaDon> listhd = hd.TimHoaDonTheoTTTinhTien(false);
        //        string mahd = "";
        //        foreach (var item in listhd)
        //        {
        //            if (item.TenBan == lvBanCaFe.SelectedItems[0].SubItems[0].Text)
        //                mahd = item.MaHD;
        //        }
        //        if (mahd != "")
        //        {
        //            IChiTietHoaDonRepository cthd = new ChiTietHoaDonRepository();
        //            IQueryable<ChiTietHoaDon> listcthd = cthd.getByMaHD(mahd);
        //            foreach (ChiTietHoaDon ite in listcthd)
        //            {
        //                IHangHoaRepository hh = new HangHoaRepository();
        //                HangHoa h = hh.GetByMaHangHoa(ite.HangHoa);
        //                ListViewItem it = new ListViewItem(h.TenHH);
        //                it.SubItems.Add(ite.SoLuong.ToString());
        //                it.SubItems.Add((ite.SoLuong * h.GiaBan).ToString());
        //                lvDanhSachCacMonDaGoi.Items.Add(it);
        //            }
        //        }
        //    }
        //}
        #endregion
        #region
        // các xử lý trên tabQl nhà cung cấp
        //public bool KiemTraThongTinTimKiemNCC(ref string s)
        //{
        //    if (cbbKieuTimKIemNCC.SelectedIndex == 0)
        //        s += "bạn chưa chọn kiểu tìm kiếm";
        //    if (XuLyChuoiVao.CatKhoangTrangDu(txtThongTinTimKiemNCC.Text) == "")
        //        s += "bạn chưa nhập thông tin tìm kiếm";
        //    if (cbbKieuTimKIemNCC.SelectedIndex == 4 && !XuLyChuoiVao.KiemTraSDT(txtThongTinTimKiemNCC.Text))
        //        s += "số điện thoại phải bắt đầu 0 và dài 10-11 số";
        //    if (s == "")
        //        return true;
        //    return false;
        //}
         
        //private void btnThemNCC_Click(object sender, EventArgs e)
        //{
        //    FormThemNCC frm = new FormThemNCC();
        //    frm.ShowDialog();
        //}



        #endregion
        // HÀNG HÓA
#region


        
        // các xử lý trên tab QL Hàng Hóa


        public static int kt=0;
      
        private void btnThemHangHoa_Click(object sender, EventArgs e)
        {
            kt = 1;
            FormThemHangHoa frm = new FormThemHangHoa();
            frm.ShowDialog();
            LoadListView(hanghoa.GetAll());
           
        }
        public static string ma = null;
        public static string ten = null;

        private void btnCapNhatHangHoa_Click_1(object sender, EventArgs e)
        {
            kt = 2;
            if (lvHangHoa.SelectedItems.Count > 0)
            {
                ma = lvHangHoa.SelectedItems[0].SubItems[0].Text;
                ten = lvHangHoa.SelectedItems[0].SubItems[1].Text;
                FormThemHangHoa frm = new FormThemHangHoa();
              
                frm.ShowDialog();
                LoadListView(hanghoa.GetAll());
            }
            else
            {
                MessageBox.Show("Bạn chưa chọn hàng hóa muốn cập nhật");
            }
          
            
        }

        private void cbbTimKiemHangHoa_SelectedIndexChanged(object sender, EventArgs e)
        {
            int chon = cbbKieuTimKiemHangHoa.SelectedIndex;
            switch (chon)
            {
                case 0:
                    lblTimKiemHangHoa.Visible = false;
                    dtpTimKiemHangHoa.Visible = false;
                    txtTimKiemHangHoa.Visible = false;
                    cbbTimKiemHangHoa.Visible = false;
                    break;
                case 1:
                    lblTimKiemHangHoa.Visible = false;
                    dtpTimKiemHangHoa.Visible = false;
                    txtTimKiemHangHoa.Visible = false;
                    cbbTimKiemHangHoa.Visible = false;
                    break;
                case 2:
                    lblTimKiemHangHoa.Visible = true;
                    txtTimKiemHangHoa.Visible = true;
                    cbbTimKiemHangHoa.Visible = false;
                    dtpTimKiemHangHoa.Visible = false;
                    cbbTimKiemHangHoa.Items.Clear();
                    
                    break;
                case 3:
                    lblTimKiemHangHoa.Visible = true;
                    cbbTimKiemHangHoa.Visible = true;
                    dtpTimKiemHangHoa.Visible = false;
                    txtTimKiemHangHoa.Visible = false;
                    cbbTimKiemHangHoa.Items.Add("---chọn loại hàng hóa---");
                    //cbbTimKiemHangHoa.Items.Add("Tất cả");
                    cbbTimKiemHangHoa.Items.Add("Nguyên liệu");
                    cbbTimKiemHangHoa.Items.Add("Không chế biến");
                    cbbTimKiemHangHoa.Items.Add("Qua chế biến");
                    cbbTimKiemHangHoa.SelectedIndex = 0;
                    break;
                case 4:
                    lblTimKiemHangHoa.Visible = true;
                    txtTimKiemHangHoa.Visible = false;
                    cbbTimKiemHangHoa.Visible = true;
                    dtpTimKiemHangHoa.Visible = false;
                    cbbTimKiemHangHoa.Items.Clear();
                    cbbTimKiemHangHoa.Items.Add("--chọn nhà cung cấp---");
                   // cbbTimKiemHangHoa.Items.Add("Tất cả");
                    LoadNhaCungCap();
                    cbbTimKiemHangHoa.SelectedIndex = 0;
                    break;
                case 5:
                    lblTimKiemHangHoa.Visible = true;
                    txtTimKiemHangHoa.Visible = false;
                    cbbTimKiemHangHoa.Visible = false;
                    dtpTimKiemHangHoa.Visible = true;
                    break;
                case 6:
                    lblTimKiemHangHoa.Visible = true;
                    txtTimKiemHangHoa.Visible = true;
                    cbbTimKiemHangHoa.Visible = false;
                    dtpTimKiemHangHoa.Visible = false;
                    break;
            }
        }
        public bool KiemTraThongTinTimKiemHangHoa(ref string s)
        {
            string st = XuLyChuoiVao.CatKhoangTrangDu(txtTimKiemHangHoa.Text);
            int t = cbbKieuTimKiemHangHoa.SelectedIndex;
            if (t == -1)
                s += "Bạn chưa chọn kiểu tìm kiếm hàng hóa";
            if (st == "")
            {
                if (t == 2)
                    s += "bạn chưa nhập tên hàng hóa cần tìm";
                if (t == 6)
                    s += "bạn chưa nhập số lượng hàng hóa cần tìm";
            }
            if (cbbTimKiemHangHoa.SelectedIndex <= 0)
            {
                if (t == 3)
                    s += "bạn chưa chọn loại hàng hóa";
                if (t == 4)
                    s += "bạn chưa chọn nhà cung cấp";
            }
            if (t == 6 && !XuLyChuoiVao.KiemTraSo(txtTimKiemHangHoa.Text))
                s += "bạn phải nhập số lượng";
            DateTime dt = DateTime.Now;
            DateTime dttk = dtpTimKiemHangHoa.Value;
            if (s == "")
                return true;
            return false;
        }
 

        private void btnTimKiemHangHoa_Click(object sender, EventArgs e)
        {
            string s="";
            IHangHoaRepository hanghh = new HangHoaRepository();
            int chon = cbbKieuTimKiemHangHoa.SelectedIndex;
            if(!KiemTraThongTinTimKiemHangHoa(ref s))
            {
                MessageBox.Show(s);
            }
            else
            {
               // MessageBox.Show("bạn chưa xử lý tìm kiếm");
                switch(chon)
                {
                    case 0:
                        break;
                    case 1:
                        lvHangHoa.Items.Clear();
                        IQueryable<HangHoa> hht = hanghh.GetAll();
                        LoadListView(hht);
                        break;
                    case 2:
                        // Tim Kiem hang Hoa Theo Ten 
                            lvHangHoa.Items.Clear();
                            //IHangHoaRepository hanghh = new HangHoaRepository();
                        IQueryable<HangHoa> hhten = hanghh.TimHangHoaTheoTen(txtTimKiemHangHoa.Text);
                        LoadListView(hhten);
                        break;
                    case 3:
                        //string loai = cbbTimKiemHangHoa.Text;
                        IQueryable<HangHoa> listt = hanghh.TimHangHoaTheoLoai(cbbTimKiemHangHoa.Text);
                        LoadListView(listt);
                    

                        break;
                    case 4:

                        //tim kiem hh theo nha cung cap
                        NhaCungCapRepository ncc=new NhaCungCapRepository();
                        string vd = cbbTimKiemHangHoa.Text;
                        NhaCungCap cc = ncc.GetByIdNhaCungCap(vd);
                        HangHoaRepository hh=new HangHoaRepository();
                        IQueryable<HangHoa> list = hh.TimHHTheoNhaCungCap(cc.MaNCC);
                        if (list != null)
                        {
                            LoadListView(list);
                        }
                        else
                        {


                            MessageBox.Show("Không có hàng hóa bạn thuộc nhà cung cấp này");
                        }
                       
                        break;
                    case 5:
                        //tim kiem hang hoa theo ngay het han
                        lvHangHoa.Items.Clear();
                         //string xau = string.Format("{0:dd/MM/yyyy}", dtpTimKiemHangHoa.Value);
                         string ngay = dtpTimKiemHangHoa.Value.ToShortDateString();
                        HangHoaRepository hhngay = new HangHoaRepository();
                        IQueryable<HangHoa> listngayhh = hhngay.TimHHTheoNgayhetHan(ngay);
                           LoadListView(listngayhh);
                           //MessageBox.Show(a.TenHH);

                        break;
                    case 6:
                        // tim kiem hang hoa theo soluong
                        lvHangHoa.Items.Clear();
                        IHangHoaRepository hangh = new HangHoaRepository();
                        int t = int.Parse(txtTimKiemHangHoa.Text);
                        IQueryable<HangHoa> hhsol = hangh.TimHangHoaTheoSoLuong(t);
                        LoadListView(hhsol);
                        break;
                }

            }
        
        }

        //private void bttthongke_Click(object sender, EventArgs e)
        //{
        //    FormThongKeBangChay frm = new FormThongKeBangChay();
        //    frm.ShowDialog();
        //}

        private void btnXoaHangHoa_Click(object sender, EventArgs e)
        {
            if (lvHangHoa.SelectedItems.Count > 0)
            {
                String ma = lvHangHoa.SelectedItems[0].SubItems[0].Text;

                HangHoa a = hanghoa.GetByMaHangHoa(ma);
                if (hanghoa.Delete(a))
                {
                    MessageBox.Show("Xóa thành công");
                    LoadListView(hanghoa.GetAll());
                }
                else
                {
                    MessageBox.Show("Lỗi");
                }
            }
            else
            {
                MessageBox.Show("Bạn chua chọn dữ lệu muốn xóa");
            }

        }
        public void LoadNhaCungCap()
        {
            NhaCungCapRepository cc=new NhaCungCapRepository();
            IQueryable<NhaCungCap> list = cc.GetAll();
            foreach (NhaCungCap a in list)
            {
                cbbTimKiemHangHoa.Items.Add(a);

            }
            cbbTimKiemHangHoa.DisplayMember ="TenNCC";
            cbbTimKiemHangHoa.ValueMember ="MaNCC";
            cbbTimKiemHangHoa.SelectedIndex = -1;

            
        }


        public void LoadListView(IQueryable<HangHoa> list)
        {
            lvHangHoa.Items.Clear();
            NhaCungCapRepository nhacc = new NhaCungCapRepository();
            IQueryable<NhaCungCap> nhc = nhacc.GetAll();
            foreach (HangHoa hh in list)
            {
                NhaCungCap cc = nhacc.GetByMaNhaCungCap(hh.NhaCC);
                ListViewItem item = lvHangHoa.Items.Add(hh.MaHH);
                item.SubItems.Add(hh.TenHH);
                item.SubItems.Add(hh.DonViTinh);
                item.SubItems.Add(hh.SoLuong.ToString());
                item.SubItems.Add(hh.NgayHethan);
                item.SubItems.Add(hh.GiaMua.ToString());
                item.SubItems.Add(hh.GiaBan.ToString());
                if (cc != null)
                {
                    item.SubItems.Add(cc.TenNCC);
                }
                else
                {
                    item.SubItems.Add("");
                }
            }
        }

        #endregion
        // KHÁCH HÀNG

       


#region

        private void bttThemKhachhang_Click(object sender, EventArgs e)
        {


            FormThemKhachHang.kiemtra = false;
            FormThemKhachHang frm = new FormThemKhachHang();
            frm.FormClosed += new FormClosedEventHandler(frm_FormClosed);
            frm.ShowDialog();
        }

        void frm_FormClosed(object sender, FormClosedEventArgs e)
        {
            LoadListView(khs.GetAll().ToList());
        }

     
        

        public IKhachHangRepository khs = new KhachHangRepository();
    
        public string makh;
        public static KhachHang khstcapNhat = new KhachHang();
        public void LoadListView(List<KhachHang> listds)
        {
            listViewKhachhang.Items.Clear();
            foreach (KhachHang kh in listds)
            {
                ListViewItem item = new ListViewItem(kh.MaKH);
                item.SubItems.Add(kh.TenKH);
                string gt = "Nam";
                if (kh.GioiTinh)
                    gt = "Nu";

                item.SubItems.Add(gt);
                item.SubItems.Add(kh.DiaChi);
                item.SubItems.Add(kh.SoDT);
                item.SubItems.Add(kh.CMND);
                item.SubItems.Add(kh.NgaySinh.ToString("dd/MM/yyyy"));
                listViewKhachhang.Items.Add(item);

            }
        }
        public enum KieuTim
        {
            SoDT,
            HoTen,
            DiaChi

        }
        public KieuTim kieutim;
        private void radten_CheckedChanged(object sender, EventArgs e)
        {

            RadioButton rd = sender as RadioButton;
            switch (rd.Name)
            {
                case "radten":
                    lbtim.Text = "Nhập Tên: ";
                    kieutim = KieuTim.HoTen;

                    break;
              
               
                default:
                    break;
            }

        }
        private void raddiachi_CheckedChanged(object sender, EventArgs e)
        {
               RadioButton rd = sender as RadioButton;
               switch (rd.Name)
               {
                   case "raddiachi":
                       lbtim.Text = "Nhập Địa Chỉ : ";
                       kieutim = KieuTim.DiaChi;
                       break;
               }
        }
         private void radsdt_CheckedChanged(object sender, EventArgs e)
        {
              RadioButton rd = sender as RadioButton;
               switch (rd.Name)
               {
                   case "radsdt":
                       lbtim.Text = "Nhập Số ĐT: ";
                       kieutim = KieuTim.SoDT;
                       break;
               }
              
        }
        private void btntim_Click(object sender, EventArgs e)
        {
            string data = txttimkiem.Text;
            switch (kieutim)
            {
                case KieuTim.SoDT:
                    List<KhachHang> ds1 = new List<KhachHang>();


                    ds1 = khs.FindSoDT(data).ToList();
                    if (ds1.Count > 0)
                    {
                        LoadListView(ds1);
                        if (data == "")
                            MessageBox.Show("Bạn Chưa Nhập Số Điện Thoại Của Khách Hàng Cần Tìm");
                    }

                    else

                        MessageBox.Show("Không Tốn Tại Khách Hàng Cần Tìm");

                    break;
                case KieuTim.HoTen:
                    List<KhachHang> dsht = new List<KhachHang>();
                    dsht = khs.FindTenKH(data).ToList();
                    if (dsht.Count > 0)
                    {
                        LoadListView(dsht);
                        if (data == "")
                            MessageBox.Show("Bạn Chưa Nhập Tên Của Khách Hàng Cần Tìm");
                    }
                    else
                        MessageBox.Show("Không Tốn Tại Khách Hàng Cần Tìm");

                    break;
                case KieuTim.DiaChi:
                    List<KhachHang> dsDc = new List<KhachHang>();
                    dsDc = khs.FindDiaChi(data).ToList();
                    if (dsDc.Count > 0)
                    {
                        LoadListView(dsDc);
                        if (data == "")
                            MessageBox.Show("Bạn Chưa Nhập Địa Chỉ Của Khách Hàng Cần Tìm");
                    }
                    else
                        MessageBox.Show("Không Tốn Tại Khách Hàng Cần Tìm");
                    break;
                default:
                    break;
            }

        }
       
        private void cậpNhậtThôngTinKháchHàngToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            khstcapNhat = khs.GetById(makh);
            FormThemKhachHang.khag = khstcapNhat;
            FormThemKhachHang.kiemtra = true;
            FormThemKhachHang frm = new FormThemKhachHang();
            frm.FormClosed += new FormClosedEventHandler(frm_FormClosed);
            frm.ShowDialog();

        }

        private void bttXoa_Click(object sender, EventArgs e)
        {
            DialogResult rs = MessageBox.Show("Bạn có thật sự muốn xóa khách hàng này không", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (rs == DialogResult.Yes)
            {
                KhachHang khsa = khs.GetById(makh);
                bool xoa = khs.Delete(khsa);

                if (xoa)
                {
                    MessageBox.Show("Xóa Thành Công");
                    LoadListView(khs.GetAll().ToList());
                }

                else
                    MessageBox.Show("Thất Bại ");

            }
        }
        private void xóaKháchHàngToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            DialogResult rs = MessageBox.Show("Bạn có thật sự muốn xóa khách hàng này không", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (rs == DialogResult.Yes)
            {
                KhachHang khsa = khs.GetById(makh);
                bool xoa = khs.Delete(khsa);

                if (xoa)
                {
                    MessageBox.Show("Xóa Thành Công");
                    LoadListView(khs.GetAll().ToList());
                }

                else
                    MessageBox.Show("Thất Bại ");

            }
        }

        private void cậpNhậtThôngTinKháchHàngToolStripMenuItem1_Click(object sender, EventArgs e)
        {


            khstcapNhat = khs.GetById(makh);
            FormThemKhachHang.khag = khstcapNhat;
            FormThemKhachHang.kiemtra = true;
            FormThemKhachHang frm = new FormThemKhachHang();
            frm.FormClosed += new FormClosedEventHandler(frm_FormClosed);
            frm.ShowDialog();

        }
        private void bttCapNhat_Click_1(object sender, EventArgs e)
        {
            
            khstcapNhat = khs.GetById(makh);
            FormThemKhachHang.khag = khstcapNhat;
            FormThemKhachHang.kiemtra = true;
            FormThemKhachHang frm = new FormThemKhachHang();
            frm.FormClosed += new FormClosedEventHandler(frm_FormClosed);
            frm.ShowDialog();
        }

        private void listViewKhachhang_SelectedIndexChanged(object sender, EventArgs e)
        {
     
            if (listViewKhachhang.SelectedItems.Count > 0)
                makh = listViewKhachhang.SelectedItems[0].SubItems[0].Text;
            if (listViewKhachhang.SelectedItems.Count < 0)
                MessageBox.Show("Bạn Chọn Khách Hàng ");
        }
        private void bttLoad_Click(object sender, EventArgs e)
        {
            LoadListView(khs.GetAll().ToList());
        }

        private void btthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //public bool KiemTraThongTinTimKiemKhachHang(ref string s)
        //{
        ////    if (cbbKieuTimKiemKhachHang.SelectedIndex < 1)
        ////        s += "bạn chưa chọn kiểu tìm kiếm\n";
        ////    if (cbbKieuTimKiemKhachHang.SelectedIndex > 1 && XuLyChuoiVao.CatKhoangTrangDu(txtThongTinTimKiemKhachHang.Text) == "")
        ////        s += "bạn chưa nhập thông tin tìm kiếm\n";
        ////    if (cbbKieuTimKiemKhachHang.SelectedIndex == 4 && XuLyChuoiVao.KiemTraSDT(txtThongTinTimKiemKhachHang.Text)==false)
        ////        s += "bạn phải nhập số điện thoại và SĐT bắt đầu la 0 và dài từ 10-11 số\n";
        ////    if (s == "")
        ////        return true;
        ////    return false;
        //}

       
        

        #endregion

        // HÓA ĐƠN


        //các xử lý trên tab bàn billiards
        #region
       
        private void lvDanhSachMH_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbbSoLuongMH.SelectedIndex = 0;
        }

        private void btnThemMH_Click(object sender, EventArgs e)
        {
            HangHoaRepository hanghoa = new HangHoaRepository();
            int i = lvDanhSachMH.SelectedItems.Count - 1;
            if (i >= 0)
            {
                if (!KiemTraMatHangDaCoBl(lvDanhSachMH.SelectedItems[i].SubItems[0].Text))
                {
                    HangHoa hh = hanghoa.TimTheoTen(lvDanhSachMH.SelectedItems[i].SubItems[0].Text);
                    ListViewItem lv = new ListViewItem(lvDanhSachMH.SelectedItems[i].SubItems[0].Text);
                    lv.SubItems.Add(cbbSoLuongMH.Text);
                    int tien = int.Parse(cbbSoLuongMH.Text) * hh.GiaBan;
                    lv.SubItems.Add(tien.ToString());
                    lvDSMHDaGoi.Items.Add(lv);


                    i--;
                }
                else
                {
                    for (int j = 0; j < lvDSMHDaGoi.Items.Count; j++)
                        if (lvDSMHDaGoi.Items[j].SubItems[0].Text == lvDanhSachMH.SelectedItems[i].SubItems[0].Text)
                        {
                            int sl = int.Parse(lvDSMHDaGoi.Items[j].SubItems[1].Text) + int.Parse(cbbSoLuongMH.Text);
                            lvDSMHDaGoi.Items[j].SubItems[1].Text = sl.ToString();

                            HangHoa hh = hanghoa.TimTheoTen(lvDanhSachMH.SelectedItems[i].SubItems[0].Text);
                            int tien = int.Parse(lvDSMHDaGoi.Items[j].SubItems[1].Text) * hh.GiaBan;
                            lvDSMHDaGoi.Items[j].SubItems[2].Text = tien.ToString();


                        }
                }

            }
            TinhTienTheoBanBL();
        }
        private void btnGiamMH_Click(object sender, EventArgs e)
        {
            int i = lvDSMHDaGoi.SelectedItems.Count - 1;
            if (i >= 0)
            {
                if (lvDSMHDaGoi.SelectedItems[i].SubItems[1].Text == "1")
                {
                    for (int j = 0; j < lvDSMHDaGoi.Items.Count; j++)
                        if (lvDSMHDaGoi.Items[j].SubItems[0].Text == lvDSMHDaGoi.SelectedItems[i].SubItems[0].Text)
                            lvDSMHDaGoi.Items.RemoveAt(j);

                }
                else
                {
                    for (int j = 0; j < lvDSMHDaGoi.Items.Count; j++)
                    {
                        if (lvDSMHDaGoi.Items[j].SubItems[0].Text == lvDSMHDaGoi.SelectedItems[i].SubItems[0].Text)
                        {

                            if (int.Parse(cbbSoLuongMH.Text) - int.Parse(lvDSMHDaGoi.Items[j].SubItems[1].Text) < 0)
                            {

                                int sl = int.Parse(lvDSMHDaGoi.Items[j].SubItems[1].Text) - int.Parse(cbbSoLuongMH.Text);

                                lvDSMHDaGoi.SelectedItems[i].SubItems[1].Text = sl.ToString();

                                HangHoa hh = hanghoa.TimTheoTen(lvDSMHDaGoi.SelectedItems[i].SubItems[0].Text);
                                int tien = int.Parse(sl.ToString()) * hh.GiaBan;
                                lvDSMHDaGoi.SelectedItems[i].SubItems[2].Text = tien.ToString();


                            }
                            else
                                if (int.Parse(cbbSoLuongMH.Text) - int.Parse(lvDSMHDaGoi.SelectedItems[i].SubItems[1].Text) == 0)
                                {
                                    lvDSMHDaGoi.Items.RemoveAt(j);

                                    return;
                                }
                                else
                                    MessageBox.Show("so luong giam vuot qua so luong da co");
                        }
                    }
                }
            }
            TinhTienTheoBanBL();
        }
        public void TinhTienTheoBanBL()
        {
            int tongtien = 0;
            if (lvDanhSachCacMonDaGoi.Items.Count == 0)
            {
            }
            else
            {


                for (int i = 0; i < lvDanhSachCacMonDaGoi.Items.Count; i++)
                {
                    tongtien += int.Parse(lvDanhSachCacMonDaGoi.Items[i].SubItems[2].Text);
                }
            }
            txtThanhTien.Text = tongtien.ToString();

        }
        public bool KiemTraMatHangDaCoBl(string s)
        {
            for (int i = 0; i < lvDSMHDaGoi.Items.Count; i++)
                if (lvDSMHDaGoi.Items[i].SubItems[0].Text == s)
                {
                    return true;
                }
            return false;
        }
        #endregion
        // các xử lý trên tab QL Hóa Đơn
        #region


        private void btnThemHoaDon_Click(object sender, EventArgs e)
        {
            FormThemHoaDon frm = new FormThemHoaDon();
            frm.ShowDialog();
        }
        //t sửa lại chỗ này
        //uh 
        public bool KiemTraChuoiTrung(List<string> str, string s)
        {
            foreach (string item in str)
                if (s == item)
                    return true;
            return false;
        }
        private void cbbTimKiemHoaDon_SelectedIndexChanged(object sender, EventArgs e)
        {
            int chon = cbbTimKiemHoaDon.SelectedIndex;
            switch (chon)
            {
                case 0:
                    txtThongTinTimKiemHoaDon.Visible = false;
                    lblTimKiemHoaDon.Visible = false;
                    dtpTimKiemHoaDon.Visible = false;
                    lblNguoiLap.Visible = false;
                    cbbNguoiLap.Visible = false;
                    rdHDBan.Visible = false;
                    rdHDMua.Visible = false;
                    break;
                case 1:
                    LoadBanHD();
                    txtThongTinTimKiemHoaDon.Visible = false;
                    lblTimKiemHoaDon.Visible = false;
                    dtpTimKiemHoaDon.Visible = false;
                    cbbNguoiLap.Visible = true;
                    lblNguoiLap.Visible = true;
                    rdHDBan.Visible = false;
                    rdHDMua.Visible = false;
                    lblNguoiLap.Text = "Tên Bàn";
                    break;
                case 2:
                    LoadNguoiLap();
                    txtThongTinTimKiemHoaDon.Visible = false;
                    lblTimKiemHoaDon.Visible = false;
                    dtpTimKiemHoaDon.Visible = false;
                    cbbNguoiLap.Visible = true;
                    lblNguoiLap.Visible = true;
                    rdHDBan.Visible = false;
                    rdHDMua.Visible = false;
                    lblNguoiLap.Text = "Người Lập";
                    break;
                case 3:
                    txtThongTinTimKiemHoaDon.Visible = false;
                    lblTimKiemHoaDon.Visible = false;
                    dtpTimKiemHoaDon.Visible = false;
                    cbbNguoiLap.Visible =false ;
                    lblNguoiLap.Visible =false ;
                    rdHDBan.Visible = true;
                    rdHDMua.Visible = true;
                    rdHDBan.Checked = true;
                    lblNguoiLap.Text  = "Loại";
                    break;
                case 4:
                    txtThongTinTimKiemHoaDon.Visible = false;
                    lblTimKiemHoaDon.Visible = true;
                    dtpTimKiemHoaDon.Visible = true;
                    cbbNguoiLap.Visible = false;
                    lblNguoiLap.Visible = false;
                    rdHDBan.Visible = false;
                    rdHDMua.Visible = false;
                    lblTimKiemHoaDon.Text = "Ngày";
                    break;
                case 5:
                    LoadThangTK();
                    txtThongTinTimKiemHoaDon.Visible = true;
                    lblTimKiemHoaDon.Visible = true;
                    dtpTimKiemHoaDon.Visible = false;
                    cbbNguoiLap.Visible = true;
                    lblNguoiLap.Visible = true;
                    rdHDBan.Visible = false;
                    rdHDMua.Visible = false;
                    lblTimKiemHoaDon.Text = "Năm";
                    lblNguoiLap.Text = "Tháng";
                    break;
                case 6:
                    LoadQuyTK();
                    lblTimKiemHoaDon.Visible = true;
                    dtpTimKiemHoaDon.Visible = false;
                    cbbNguoiLap.Visible = true;
                    lblNguoiLap.Visible = true;
                    rdHDBan.Visible = false;
                    rdHDMua.Visible = false;
                    txtThongTinTimKiemHoaDon.Visible = true;
                    lblTimKiemHoaDon.Text = "Năm";
                    lblNguoiLap.Text = "Quý";
                    break;
                case 7:
                    lblTimKiemHoaDon.Visible = true;
                    dtpTimKiemHoaDon.Visible =false;
                    cbbNguoiLap.Visible = false;
                    lblNguoiLap.Visible = false;
                    rdHDBan.Visible = false;
                    rdHDMua.Visible = false;
                    txtThongTinTimKiemHoaDon.Visible = true;
                    lblTimKiemHoaDon.Text = "Năm";
                    break;
            }
        }
        public void LoadNguoiLap()
        {
            TaiKhoanRepository tk = new TaiKhoanRepository();
            IQueryable<TaiKhoan> ds = tk.GetAll();
            cbbNguoiLap.Items.Clear();
            //HoaDonRepository hd = new HoaDonRepository();
            //IQueryable<HoaDon> ds = hd.GetAll();
            foreach (TaiKhoan a in ds)
            {
                cbbNguoiLap.Items.Add(a);

            }

            cbbNguoiLap.DisplayMember = "TenUser";
            cbbNguoiLap.ValueMember = "UserName";
            cbbNguoiLap.SelectedIndex = 0;
        }
        private void LoadBanHD()
        {
            cbbNguoiLap.Items.Clear();
            BanRepository b = new BanRepository();

            IQueryable<Ban> list = b.GetAll();
            
            foreach (Ban a in list)
            {
                cbbNguoiLap.Items.Add(a);
            }
            cbbNguoiLap.DisplayMember = "TenBan";
            cbbNguoiLap.ValueMember = "TenBan";
            cbbNguoiLap.SelectedIndex = 0;
        }
        private void LoadQuyTK()
        {
            cbbNguoiLap.Items.Clear();
            cbbNguoiLap.Items.Add("Quý I");
            cbbNguoiLap.Items.Add("Quý II");
            cbbNguoiLap.Items.Add("Quý III");
            cbbNguoiLap.Items.Add("Quý IV");
            cbbNguoiLap.SelectedIndex = 0;
        }
        
        //private IQueryable<HoaDon> TimHoaDonTheoQuy()
        //{
        //    string[] listthang = new string[10];
        //    string buon = "";
        //    IHoaDonRepository hd = new HoaDonRepository();
        //    //IQueryable<HoaDon> kq=new IQueryable<HoaDon>();
        //    if (cbbNguoiLap.Text == "Quý I")
        //    {
        //        string asd = "01 02 03";
        //        listthang = asd.Split(' ');

        //    }
        //    else
        //    {

        //        if (cbbNguoiLap.Text == "Quý II")
        //        {
        //            string adf = "04 05 06";
        //            listthang = adf.Split(' ');
        //        }
        //        else
        //        {
        //            if (cbbNguoiLap.Text == "Quý III")
        //            {
        //                string asfasf = "07 08 09";
        //                listthang = asfasf.Split(' ');
        //            }
        //            string safras = "10 11 12";
        //            listthang = safras.Split(' ');
        //        }
        //    }
        //    for (int j = 0; j < 3; j++)
        //    {
        //        buon = listthang[j] + "/" + txtThongTinTimKiemHoaDon.Text;
                
                
        //    }
        //    IQueryable<HoaDon> kq = hd.TimHoaDonTheoThang(buon);
        //    return kq;
        //}
        private void LoadThangTK()
        {
            cbbNguoiLap.Items.Clear();
            cbbNguoiLap.Items.Add("01");
            cbbNguoiLap.Items.Add("02");
            cbbNguoiLap.Items.Add("03");
            cbbNguoiLap.Items.Add("04");
            cbbNguoiLap.Items.Add("05");
            cbbNguoiLap.Items.Add("06");
            cbbNguoiLap.Items.Add("07");
            cbbNguoiLap.Items.Add("08");
            cbbNguoiLap.Items.Add("09");
            cbbNguoiLap.Items.Add("10");
            cbbNguoiLap.Items.Add("11");
            cbbNguoiLap.Items.Add("12");
            cbbNguoiLap.SelectedIndex = 0;
        }
        private void btnOKTimKiemHoaDon_Click(object sender, EventArgs e)
        {
            int chon = cbbTimKiemHoaDon.SelectedIndex;
            switch (chon)
            {
                case 0:
                    lvDSHoaDon.Items.Clear();
                    IHoaDonRepository anhhangaa = new HoaDonRepository();
                    LoadLVDanhSachHoaDon(anhhangaa.GetAll());
                    break;
                case 1:
                    lvDSHoaDon.Items.Clear();
                    IHoaDonRepository hdon=new HoaDonRepository();
                    IQueryable<HoaDon> hd = hdon.TimHoaDonTheoBan(cbbNguoiLap.Text);
                    if (hd != null)
                    {
                        LoadLVDanhSachHoaDon(hd);

                    }
                    else
                    {

                        MessageBox.Show("Không có hóa đơn");
                    }
                    
                    break;
                case 2:
                    lvDSHoaDon.Items.Clear();
                    IHoaDonRepository hod = new HoaDonRepository();
                    IQueryable<HoaDon> hoa = hod.TimHoaDonTheoNguoiLap(cbbNguoiLap.Text);
                    if (hoa != null)
                    {
                        LoadLVDanhSachHoaDon(hoa);

                    }
                    else
                    {

                        MessageBox.Show("Không có hóa đơn");
                    }
                    
                    break;
                case 3:
                    lvDSHoaDon.Items.Clear();
                    IHoaDonRepository hoad = new HoaDonRepository();
                    bool kt = KiemTraLoaiHoaDon();
                    IQueryable<HoaDon> hang = hoad.TimHoaDonTheoLoai(kt);
                    LoadLVDanhSachHoaDon(hang);
                    break;
                case 4:
                    lvDSHoaDon.Items.Clear();
                    HoaDonRepository hoas = new HoaDonRepository();
                    string nga = dtpTimKiemHoaDon.Value.ToShortDateString();
                    IQueryable<HoaDon> hdn = hoas.TimHoaDonTheoNgay(nga);
                    if (hdn != null)
                    {
                        LoadLVDanhSachHoaDon(hdn);

                    }
                    else
                    {

                        MessageBox.Show("Không có hóa đơn");
                    }
                    break;
                case 5:
                    //lvDSHoaDon.Items.Clear();
                    ////lay ngay
                    //IHoaDonRepository dsfa = new HoaDonRepository();
                    
                    //MessageBox.Show(dtpTimKiemHoaDon.Value.Day.ToString());
                    lvDSHoaDon.Items.Clear();
                    string chanlam=cbbNguoiLap.Text+"/"+txtThongTinTimKiemHoaDon.Text;
                    IHoaDonRepository hdzxvfzx = new HoaDonRepository();
                    IQueryable<HoaDon> listhd = hdzxvfzx.TimHoaDonTheoThang(chanlam);
                    //IQueryable<HoaDon>listhd=hdzxvfzx.
                    if (listhd != null)
                    {
                        
                        LoadLVDanhSachHoaDon(listhd);

                    }
                    else
                    {

                        MessageBox.Show("Không có hóa đơn");
                    }
                    break;
                case 6:
                    lvDSHoaDon.Items.Clear();
                    IHoaDonRepository cana = new HoaDonRepository();
                    if (cbbNguoiLap.Text == "Quý I")
                    {
                        string aw = "01/" + txtThongTinTimKiemHoaDon.Text;
                        IQueryable<HoaDon> han = cana.TimHoaDonTheoThang(aw);
                        LoadLVDanhSachHoaDon(han);
                        string aaw = "02/" + txtThongTinTimKiemHoaDon.Text;
                        IQueryable<HoaDon> awq = cana.TimHoaDonTheoThang(aaw);
                        LoadLVDanhSachHoaDon(awq);
                        string aaaw = "03/" + txtThongTinTimKiemHoaDon.Text;
                        IQueryable<HoaDon> aqwq = cana.TimHoaDonTheoThang(aaaw);
                        LoadLVDanhSachHoaDon(aqwq);
                    }
                    else
                    {
                        if (cbbNguoiLap.Text == "Quý II")
                        {
                            string thuy = "04/" + txtThongTinTimKiemHoaDon.Text;
                            IQueryable<HoaDon> thu = cana.TimHoaDonTheoThang(thuy);
                            LoadLVDanhSachHoaDon(thu);
                            string th = "05/" + txtThongTinTimKiemHoaDon.Text;
                            IQueryable<HoaDon> nth = cana.TimHoaDonTheoThang(th);
                            LoadLVDanhSachHoaDon(nth);
                            string ntg = "06/" + txtThongTinTimKiemHoaDon.Text;
                            IQueryable<HoaDon> awqq = cana.TimHoaDonTheoThang(ntg);
                            LoadLVDanhSachHoaDon(awqq);
                        }
                        else
                        {
                            if (cbbNguoiLap.Text == "Quý III")
                            {
                                string tamt = "07/" + txtThongTinTimKiemHoaDon.Text;
                                IQueryable<HoaDon> tahu = cana.TimHoaDonTheoThang(tamt);
                                LoadLVDanhSachHoaDon(tahu);
                                string tgwegh = "08/" + txtThongTinTimKiemHoaDon.Text;
                                IQueryable<HoaDon> nqqth = cana.TimHoaDonTheoThang(tgwegh);
                                LoadLVDanhSachHoaDon(nqqth);
                                string tta = "09/" + txtThongTinTimKiemHoaDon.Text;
                                IQueryable<HoaDon> ttah = cana.TimHoaDonTheoThang(tta);
                                LoadLVDanhSachHoaDon(ttah);
                            }
                            else
                            {
                                if (cbbNguoiLap.Text == "Quý IV")
                                {
                                    string tquy = "10/" + txtThongTinTimKiemHoaDon.Text;
                                    IQueryable<HoaDon> tagfhu = cana.TimHoaDonTheoThang(tquy);
                                    LoadLVDanhSachHoaDon(tagfhu);
                                    string twqqh = "11/" + txtThongTinTimKiemHoaDon.Text;
                                    IQueryable<HoaDon> qwv = cana.TimHoaDonTheoThang(twqqh);
                                    LoadLVDanhSachHoaDon(qwv);
                                    string naswqtg = "12/" + txtThongTinTimKiemHoaDon.Text;
                                    IQueryable<HoaDon> anhasng = cana.TimHoaDonTheoThang(naswqtg);
                                    LoadLVDanhSachHoaDon(anhasng);
                                }
                                else
                                {
                                    MessageBox.Show("Không có hóa đơn");
                                }
                            }
                        }
                    }
                    
                    break;
                case 7:
                    lvDSHoaDon.Items.Clear();
                    IHoaDonRepository nguyetga=new HoaDonRepository();
                    IQueryable<HoaDon> aaaaaaa = nguyetga.TimHoaDonTheoNam(txtThongTinTimKiemHoaDon.Text);
                    LoadLVDanhSachHoaDon(aaaaaaa);

                    //if (aaa != null)
                    //{

                    //    LoadLVDanhSachHoaDon(aaa);

                    //}
                    //else
                    //{

                    //    MessageBox.Show("Không có hóa đơn");
                    //}
                    
                    break;
            }
        }
        private bool KiemTraLoaiHoaDon()
        {
            bool kq = true;
            if(rdHDMua.Checked)
                kq=false;
            //if (rdHDBan.Checked)
            //    return true;
            //else
            //    kq = false;
            return kq;
        }
        private void LoadLVDanhSachHoaDon(IQueryable<HoaDon> list)
        {
            lvDSHoaDon.Items.Clear();
            HoaDonRepository hd = new HoaDonRepository();
            foreach (HoaDon a in list)
            {

                ListViewItem ite = new ListViewItem(a.MaHD);
                ite.SubItems.Add(a.LoaiHD.ToString());
                ite.SubItems.Add(a.NguoiLap);
                ite.SubItems.Add(a.NgayLap.ToString());
                ite.SubItems.Add(a.KhachHang);
                ite.SubItems.Add(a.NhaCungCap);
                ite.SubItems.Add(a.TenBan);
                ite.SubItems.Add(a.TongTien.ToString());
                lvDSHoaDon.Items.Add(ite);
            }
        }
        #endregion


        // THỐNG KÊ DOANH THU
 #region

        public static string MaHD="";
        private void xemChiTietHoaDonToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            if (lvDSHoaDon.SelectedItems.Count > 0)
            {
                MaHD = lvDSHoaDon.SelectedItems[0].SubItems[0].Text;
                FormChiTietHoaDon frm = new FormChiTietHoaDon();
                frm.ShowDialog();
            }
            else
            {
                MessageBox.Show("Bạn chưa chọn hóa đơn muốn xem");
            }
        }

        private void capnhatHDToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (lvDSHoaDon.SelectedItems.Count > 0)
            {
            }
            else
            {
                MessageBox.Show("Bạn chưa chọn hóa đơn muốn cập nhật");
            }
        }

        private void xoaHDToolStripMenuItem_Click(object sender, EventArgs e)
        {
         DialogResult gr=  MessageBox.Show("Thông báo", "Bạn có chắc chắn muốn xóa hóa đơn này?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
         if (DialogResult.OK == gr)
         {
             string ma=lvDSHoaDon.SelectedItems[0].SubItems[0].Text;
             IHoaDonRepository hd = new HoaDonRepository();
             IChiTietHoaDonRepository cthd = new ChiTietHoaDonRepository();
             IQueryable<ChiTietHoaDon> listcthd = cthd.getByMaHD(ma);
             if (listcthd.Count<ChiTietHoaDon>() > 0)
             {
                 foreach (var item in listcthd)
                 {
                     cthd.Delete(item);
                 }
             }
                HoaDon h=hd.GetByMaHoaDon(ma);
                if (!hd.Delete(h))
                    MessageBox.Show("Xóa hóa đơn không thành công");
                else
                {
                    MessageBox.Show("Bạn đã xóa thành công");
                    LoadLVDanhSachHoaDon(hd.GetAll());
                }
            }
         
        }
        #endregion
        // thống kê doanh thu
        #region

        // các xử lý trên tab Quản lý thống kê doanh thu

        private void cbbThongKe_SelectedIndexChanged(object sender, EventArgs e)
        {
            int chon = cbbThongKe.SelectedIndex;
            switch (chon)
            {
                case 0:
                    hienThiChiTietNgayToolStripMenuItem.Visible = false;
                    hienThiChiTietThangToolStripMenuItem.Visible = false;
                    lblNamThongKe.Visible = false;
                    mtbNamThongKe.Visible = false;
                    dtpThongKeTheoNgay.Visible = true;
                    cbbThongKeTheoQuy.Visible = false;
                    lvThongKeDoanhThu.Visible = true;
                    lvThongKeHangHoa.Visible = false;
                    break;
                case 1:
                    LoadThang();
                    hienThiChiTietNgayToolStripMenuItem.Visible = false;
                    hienThiChiTietThangToolStripMenuItem.Visible = false;
                    lblNamThongKe.Visible = true;
                    mtbNamThongKe.Visible = true;
                    dtpThongKeTheoNgay.Visible = false;
                    cbbThongKeTheoQuy.Visible = true;
                    lvThongKeDoanhThu.Visible = true;
                    lvThongKeHangHoa.Visible = false;
                    break;
                case 2:
                    LoadQuy();
                    hienThiChiTietNgayToolStripMenuItem.Visible = true;
                    hienThiChiTietThangToolStripMenuItem.Visible = false;
                    lblNamThongKe.Visible = true;
                    mtbNamThongKe.Visible = true;
                    dtpThongKeTheoNgay.Visible = false;
                    cbbThongKeTheoQuy.Visible = true;
                    lvThongKeDoanhThu.Visible = true;
                    lvThongKeHangHoa.Visible = false;
                    break;
                case 3:
                     hienThiChiTietNgayToolStripMenuItem.Visible = true;
                    hienThiChiTietThangToolStripMenuItem.Visible = true;
                    lblNamThongKe.Visible = true;
                    mtbNamThongKe.Visible = true;
                    dtpThongKeTheoNgay.Visible = false;
                    cbbThongKeTheoQuy.Visible = false;
                    lvThongKeDoanhThu.Visible = true;
                    lvThongKeHangHoa.Visible = false;
                    break;
            }
        }

        private void LoadQuy()
        {
            cbbThongKeTheoQuy.Items.Clear();
            cbbThongKeTheoQuy.Items.Add("Quý I");
            cbbThongKeTheoQuy.Items.Add("Quý II");
            cbbThongKeTheoQuy.Items.Add("Quý III");
            cbbThongKeTheoQuy.Items.Add("Quý IV");
            cbbThongKeTheoQuy.SelectedIndex = 0;
        }

        private void LoadThang()
        {
            cbbThongKeTheoQuy.Items.Clear();
            cbbThongKeTheoQuy.Items.Add("01");
            cbbThongKeTheoQuy.Items.Add("02");
            cbbThongKeTheoQuy.Items.Add("03");
            cbbThongKeTheoQuy.Items.Add("04");
            cbbThongKeTheoQuy.Items.Add("05");
            cbbThongKeTheoQuy.Items.Add("06");
            cbbThongKeTheoQuy.Items.Add("07");
            cbbThongKeTheoQuy.Items.Add("08");
            cbbThongKeTheoQuy.Items.Add("09");
            cbbThongKeTheoQuy.Items.Add("10");
            cbbThongKeTheoQuy.Items.Add("11");
            cbbThongKeTheoQuy.Items.Add("12");
            cbbThongKeTheoQuy.SelectedIndex = 0;
        }

        private void btnInThongKe_Click(object sender, EventArgs e)
        {
            //PrintThongKe.Print();
        }

        private void rbThongKeDoanhThu_CheckedChanged_1(object sender, EventArgs e)
        {
            if (rbThongKeDoanhThu.Checked == true)
            {
                lvThongKeDoanhThu.Visible = true;
                lvThongKeHangHoa.Visible = false;
            }
            else
            {
                lvThongKeDoanhThu.Visible = false;
                lvThongKeHangHoa.Visible = true;
            }
        }

        private void btnThemBanBiza_Click(object sender, EventArgs e)
        {
            FormThemBanBillards f = new FormThemBanBillards();
            f.ShowDialog();
            LoadBanBL(dsban.GettAllBillards());
        }


        private void tabQLTaiKhoan_Click(object sender, EventArgs e)
        {

        }

        public bool KiemTraThongTinThongKe(ref string s)
        {
            s = "";
            if (dtpThongKeTheoNgay.Visible == true)
            {
                DateTime dt = dtpThongKeTheoNgay.Value; ;
                if (dt.Month > DateTime.Now.Month || dt.Year > DateTime.Now.Year)
                {
                    s += "thời gian vượt quá thởi gian hiện tại";
                }
                if (dt.Day > DateTime.Now.Day && dt.Month == DateTime.Now.Month && dt.Year == DateTime.Now.Year)
                {
                    s += "thời gian vượt quá thời gian hiện tại";
                }
            }
            if (mtbNamThongKe.Visible == true)
            {
                if (XuLyChuoiVao.CatKhoangTrangDu(mtbNamThongKe.Text) == "20")
                {
                    s += "bạn chưa nhập năm";
                }
                if (int.Parse(XuLyChuoiVao.CatKhoangTrangDu(mtbNamThongKe.Text)) > DateTime.Now.Year)
                {
                    s += "Năm vượt quá năm hiện tại";
                }
            }
            if (s == "")
                return true;
            return false;
        }

        private void btnOKThongKe_Click(object sender, EventArgs e)
        {
            string s = "";
            if (!KiemTraThongTinThongKe(ref s))
            {
                MessageBox.Show(s);
            }
            else
            {
                btnLuuThongKe.Visible = true;
                ITKDoanhThuRepository tk = new TKDoanhThuRepository();
                int chonloai = cbbThongKe.SelectedIndex;
                if (rbThongKeDoanhThu.Checked == true)
                {
                    switch (chonloai)
                    {
                        case 0:
                            ThongKeTheoNgay(dtpThongKeTheoNgay.Text);
                            break;
                        case 1:
                            string thang = cbbThongKeTheoQuy.Text + "/" + mtbNamThongKe.Text;
                            ThongKeDoanhThutheoThang(thang);
                            break;
                        case 2:
                            string quy = cbbThongKeTheoQuy.Text + "/" + mtbNamThongKe.Text;
                            ThongKeTheoQuy(quy);
                            break;
                        case 3:
                            string nam = mtbNamThongKe.Text;
                            ThongKeTheoNam(nam);
                            break;
                    }
                }
                else
                {
                    lvThongKeDoanhThu.Visible = false;
                    lvThongKeHangHoa.Visible = true;
                    switch (chonloai)
                    {
                        case 0:
                            ThongKeHangHoaTheoNgay(dtpThongKeTheoNgay.Text);
                            break;
                        case 1:
                            string thang = cbbThongKeTheoQuy.Text + "/" + mtbNamThongKe.Text;
                            ThongKeHangHoaTheoThang(thang);
                            break;
                        case 2:
                            string quy = cbbThongKeTheoQuy.Text + "/" + mtbNamThongKe.Text;
                            //ThongKeHangHoaTheoQuy(quy);
                            break;
                        case 3:
                            string nam = mtbNamThongKe.Text;
                            //ThongKeHangHoaTheoNam(nam);
                            break;
                    }
                }
            }
        }
        public void ThongKeHangHoaTheoNgay(string ngay)
        {
            IHoaDonRepository hd = new HoaDonRepository();
            IChiTietHoaDonRepository cthd = new ChiTietHoaDonRepository();
            IQueryable<HoaDon> all = hd.GetAll();
            List<string> listmaHD = new List<string>();
            List<string> listmaHDnhap = new List<string>();
            foreach (var item in all)
            {
                if (!KiemTraChuoiTrung(listmaHD, item.MaHD) && item.NgayLap == ngay && item.LoaiHD == false)
                    listmaHD.Add(item.MaHD);
            }
            foreach (var item in all)
            {
                if (!KiemTraChuoiTrung(listmaHDnhap, item.MaHD) && item.NgayLap == ngay && item.LoaiHD == true)
                    listmaHDnhap.Add(item.MaHD);
            }
            List<string> listmaHH = new List<string>();
            foreach (string mhd in listmaHD)
            {
                IQueryable<ChiTietHoaDon> listcthd = cthd.getByMaHD(mhd);
                foreach (var item in listcthd)
                {
                    if (!KiemTraChuoiTrung(listmaHH, item.HangHoa))
                        listmaHH.Add(item.HangHoa);
                }
            }
            foreach (string mahd in listmaHDnhap)
            {
                IQueryable<ChiTietHoaDon> listcthd = cthd.getByMaHD(mahd);
                foreach (var item in listcthd)
                {
                    if (!KiemTraChuoiTrung(listmaHH, item.HangHoa))
                        listmaHH.Add(item.HangHoa);
                }
            }
            if (listmaHH.Count == 0)
            {
                MessageBox.Show("Không tìm thấy hàng hóa");
            }
            else
            {
                btnInThongKe.Visible = true;
                btnLuuThongKe.Visible = true;


                int slban = 0, slmua = 0;
                foreach (string mahh in listmaHH)
                {
                    foreach (string mhd in listmaHDnhap)
                    {
                        IQueryable<ChiTietHoaDon> listcthd = cthd.getByMaHD(mhd);
                        foreach (var item in listcthd)
                        {
                            if (item.HangHoa == mahh)
                                slmua += item.SoLuong;
                        }
                    }
                    foreach (string mhd in listmaHD)
                    {
                        IQueryable<ChiTietHoaDon> listcthd = cthd.getByMaHD(mhd);
                        foreach (var item in listcthd)
                        {
                            if (item.HangHoa == mahh)
                                slban += item.SoLuong;
                        }

                    }
                    IHangHoaRepository hh = new HangHoaRepository();
                    HangHoa h = hh.GetByMaHangHoa(mahh);
                    ListViewItem itemhh = new ListViewItem(h.MaHH);
                    itemhh.SubItems.Add(h.TenHH);
                    itemhh.SubItems.Add(slmua.ToString());
                    itemhh.SubItems.Add(slban.ToString());
                    itemhh.SubItems.Add(h.GiaMua.ToString());
                    itemhh.SubItems.Add(h.GiaBan.ToString());
                    lvThongKeHangHoa.Items.Add(itemhh);
                }
            }
            TinhThuChiHangHoa();
        }
        public void ThongKeHangHoaTheoThang(string thang)
        {
            lvThongKeHangHoa.Items.Clear();
            List<string> listmahdnhap = new List<string>();
            List<string> listmahdban = new List<string>();
            List<string> listmahd = new List<string>();
            IHoaDonRepository hd = new HoaDonRepository();
            IQueryable<HoaDon> listhd = hd.GetByThang(thang);
            foreach (var item in listhd)
            {
                if ((!KiemTraChuoiTrung(listmahdnhap, item.MaHD)) && item.LoaiHD == true)
                    listmahdnhap.Add(item.MaHD);
                if ((!KiemTraChuoiTrung(listmahdban, item.MaHD)) && item.LoaiHD == false)
                    listmahdban.Add(item.MaHD);
                listmahd.Add(item.MaHD);
            }
            List<string> listdshh = new List<string>();
            foreach (string mahd in listmahd)
            {
                IChiTietHoaDonRepository cthd = new ChiTietHoaDonRepository();
                IQueryable<ChiTietHoaDon> list = cthd.getByMaHD(mahd);
                foreach (var item in list)
                {
                    if (!KiemTraChuoiTrung(listdshh, item.HangHoa))
                        listdshh.Add(item.HangHoa);
                }
            }
            if (listdshh.Count == 0)
            {
                MessageBox.Show("Không tìm thấy");
            }
            else
            {
                btnLuuThongKe.Visible = true;
                btnInThongKe.Visible = true;


                foreach (string hh in listdshh)
                {
                    int slmua = 0, slban = 0;
                    foreach (string hdn in listmahdnhap)
                    {
                        IChiTietHoaDonRepository hdnhap = new ChiTietHoaDonRepository();
                        IQueryable<ChiTietHoaDon> listcthd = hdnhap.getByMaHD(hdn);
                        foreach (var item in listcthd)
                        {
                            if (item.HangHoa == hh)
                                slmua += item.SoLuong;
                        }
                    }
                    foreach (string hdn in listmahdban)
                    {
                        IChiTietHoaDonRepository hdnhap = new ChiTietHoaDonRepository();
                        IQueryable<ChiTietHoaDon> listcthd = hdnhap.getByMaHD(hdn);
                        foreach (var item in listcthd)
                        {
                            if (item.HangHoa == hh)
                                slban += item.SoLuong;
                        }
                    }
                    IHangHoaRepository h = new HangHoaRepository();
                    HangHoa tkhh = h.GetByMaHangHoa(hh);
                    ListViewItem it = new ListViewItem(hh);
                    it.SubItems.Add(tkhh.TenHH);
                    it.SubItems.Add(slmua.ToString());
                    it.SubItems.Add(slban.ToString());
                    it.SubItems.Add(tkhh.GiaMua.ToString());
                    it.SubItems.Add(tkhh.GiaBan.ToString());
                    lvThongKeHangHoa.Items.Add(it);
                }
                TinhThuChiHangHoa();
            }
        }

        //private void btnThemMH_Click(object sender, EventArgs e)
        //{

        //}
        public void ThongKeHangHoaTheoQuy(string quy)
        {
            IHoaDonRepository hd = new HoaDonRepository();
            IQueryable<HoaDon> listhd = null;
            List<string> dsmahd = new List<string>();
            string q = quy.Substring(4, quy.Length - 9);
            string nam=quy.Substring(quy.Length-4,4);
            if (q == "I")
            {
                listhd = hd.GetByThang("01/" + nam);
                if (listhd.Count<HoaDon>() > 0)
                {
                    foreach (var item in listhd)
                    {
                        dsmahd.Add(item.MaHD);
                    }
                }
                listhd = hd.GetByThang("02/" + nam);
                if (listhd.Count<HoaDon>() > 0)
                {
                    foreach (var item in listhd)
                    {
                        dsmahd.Add(item.MaHD);
                    }
                }
                listhd = hd.GetByThang("03/" + nam);
                if (listhd.Count<HoaDon>() > 0)
                {
                    foreach (var item in listhd)
                    {
                        dsmahd.Add(item.MaHD);
                    }
                }
            }
            if (q == "II")
            {
                listhd = hd.GetByThang("04/" + nam);
                if (listhd.Count<HoaDon>() > 0)
                {
                    foreach (var item in listhd)
                    {
                        dsmahd.Add(item.MaHD);
                    }
                }
                listhd = hd.GetByThang("05/" + nam);
                if (listhd.Count<HoaDon>() > 0)
                {
                    foreach (var item in listhd)
                    {
                        dsmahd.Add(item.MaHD);
                    }
                }
                listhd = hd.GetByThang("06/" + nam);
                if (listhd.Count<HoaDon>() > 0)
                {
                    foreach (var item in listhd)
                    {
                        dsmahd.Add(item.MaHD);
                    }
                }
            }
            if (q == "III")
            {
                listhd = hd.GetByThang("07/" + nam);
                if (listhd.Count<HoaDon>() > 0)
                {
                    foreach (var item in listhd)
                    {
                        dsmahd.Add(item.MaHD);
                    }
                }
                listhd = hd.GetByThang("08/" + nam);
                if (listhd.Count<HoaDon>() > 0)
                {
                    foreach (var item in listhd)
                    {
                        dsmahd.Add(item.MaHD);
                    }
                }
                listhd = hd.GetByThang("09/" + nam);
                if (listhd.Count<HoaDon>() > 0)
                {
                    foreach (var item in listhd)
                    {
                        dsmahd.Add(item.MaHD);
                    }
                }
            }
            if (q == "IV")
            {
                listhd = hd.GetByThang("10/" + nam);
                if (listhd.Count<HoaDon>() > 0)
                {
                    foreach (var item in listhd)
                    {
                        dsmahd.Add(item.MaHD);
                    }
                }
                listhd = hd.GetByThang("11/" + nam);
                if (listhd.Count<HoaDon>() > 0)
                {
                    foreach (var item in listhd)
                    {
                        dsmahd.Add(item.MaHD);
                    }
                }
                listhd = hd.GetByThang("12/" + nam);
                if (listhd.Count<HoaDon>() > 0)
                {
                    foreach (var item in listhd)
                    {
                        dsmahd.Add(item.MaHD);
                    }
                }
            }
        }
        public void ThongKeHangHoaTheoNam(string nam)
        {
        }

        
 
 

        private void ThongKeTheoNgay(string ngay)
        {
            lvThongKeDoanhThu.Items.Clear();
            ITKDoanhThuRepository tk = new TKDoanhThuRepository();

            IHoaDonRepository hd = new HoaDonRepository();
            IQueryable<HoaDon> listhd = hd.GetByNgay(ngay);
            if (listhd.Count<HoaDon>() == 0)
                MessageBox.Show("không tìm thấy");
            else
            {
                btnLuuThongKe.Visible = true;
                btnInThongKe.Visible = true;
                int chi = 0, thu = 0, loinhuan = 0;
                foreach (var item in listhd)
                {
                    if (item.LoaiHD == true)
                        chi += item.TongTien;
                    else
                        thu += item.TongTien;
                }
                ListViewItem it = new ListViewItem(ngay);

                it.SubItems.Add(chi.ToString());
                it.SubItems.Add(thu.ToString());
                it.SubItems.Add((thu - chi).ToString());
                lvThongKeDoanhThu.Items.Add(it);
                loinhuan = thu - chi;
                TKDoanhThu tkdt = new TKDoanhThu(ngay, chi, thu);
                TKDoanhThu tktk = tk.GetByNgay(ngay);
                if (tktk != null)
                {
                    if (tktk.ThoiGianTK != tkdt.ThoiGianTK)
                        tk.AddNew(tkdt);
                }
                else
                {
                    tk.AddNew(tkdt);
                }
            }
            TinhThuChi();
        }

        public void ThongKeDoanhThutheoThang(string thang)
        {
            lvThongKeDoanhThu.Items.Clear();
            ITKDoanhThuRepository tk = new TKDoanhThuRepository();
            List<string> dsngay = new List<string>();
            IHoaDonRepository hd = new HoaDonRepository();
            IQueryable<HoaDon> list = hd.GetAll();
            foreach (var item in list)
            {
                if (!KiemTraChuoiTrung(dsngay, item.NgayLap) && item.NgayLap.Substring(item.NgayLap.Length - 7, 7) == thang)
                    dsngay.Add(item.NgayLap);
            }
            if (dsngay.Count == 0)
            {
                MessageBox.Show("Không tìm thấy!");
            }
            else
            {
                btnInThongKe.Visible = true;
                btnLuuThongKe.Visible = true;
                foreach (string st in dsngay)
                {
                    IHoaDonRepository hdn = new HoaDonRepository();
                    IQueryable<HoaDon> listhd = hdn.GetByNgay(st);
                    if (listhd.Count<HoaDon>() == 0)
                        MessageBox.Show("không tìm thấy");
                    else
                    {
                        int chi = 0, thu = 0, loinhuan = 0;
                        foreach (var item in listhd)
                        {
                            if (item.LoaiHD == true)
                                chi += item.TongTien;
                            else
                                thu += item.TongTien;
                        }
                        ListViewItem it = new ListViewItem(st);
                it.SubItems.Add(chi.ToString());
                        it.SubItems.Add(thu.ToString());
                        it.SubItems.Add((thu - chi).ToString());
                        lvThongKeDoanhThu.Items.Add(it);
                        loinhuan = thu - chi;
                        TKDoanhThu tkdt = new TKDoanhThu(st, chi, thu);
                        TKDoanhThu tktk = tk.GetByNgay(st);
                        if (tktk != null)
                        {
                            if (tktk.ThoiGianTK != tkdt.ThoiGianTK)
                                tk.AddNew(tkdt);
                        }
                        else
                            tk.AddNew(tkdt);
                    }
                }
                TinhThuChi();
            }
        }
        public void LoadThongKeThang(string thang)
        {
            int chi = 0, thu = 0, loinhuan = 0;
            ITKDoanhThuRepository tk = new TKDoanhThuRepository();
            List<string> dsngay = new List<string>();
            IHoaDonRepository hd = new HoaDonRepository();
            IQueryable<HoaDon> list = hd.GetAll();
            foreach (var item in list)
            {
                if (!KiemTraChuoiTrung(dsngay, item.NgayLap) && item.NgayLap.Substring(item.NgayLap.Length - 7, 7) == thang)
                    dsngay.Add(item.NgayLap);
            }
            foreach (string st in dsngay)
            {
                IHoaDonRepository hdn = new HoaDonRepository();
                IQueryable<HoaDon> listhd = hdn.GetByNgay(st);
                if (listhd.Count<HoaDon>() == 0)
                    MessageBox.Show("không tìm thấy");
                else
                {
                    int thungay = 0, chingay = 0;
                    foreach (var item in listhd)
                    {
                        if (item.LoaiHD == true)
                        {
                            chingay = item.TongTien;
                            chi += item.TongTien;
                        }
                        else
                        {
                            thungay = item.TongTien;
                            thu += item.TongTien;
                        }
                    }
                    TKDoanhThu tkdt = new TKDoanhThu(st, chingay, thungay);
                    TKDoanhThu tktk = tk.GetByNgay(st);
                    if (tktk != null)
                    {
                        if (tktk.ThoiGianTK != tkdt.ThoiGianTK)
                            tk.AddNew(tkdt);
                    }
                    else
                        tk.AddNew(tkdt);
                }
            }
            loinhuan = thu - chi;
            string th = "Tháng " + thang;
            ListViewItem i = new ListViewItem(th);
            i.SubItems.Add(chi.ToString());
            i.SubItems.Add(thu.ToString());
            i.SubItems.Add(loinhuan.ToString());
            lvThongKeDoanhThu.Items.Add(i);
        }
        public void ThongKeTheoQuy(string quy)
        {
            btnInThongKe.Visible = true;
            btnInThongKe.Visible = true;
            string timquy = quy.Substring(4, quy.Length - 9);
            lvThongKeDoanhThu.Items.Clear();
            if (timquy == "I")
            {
                for (int i = 1; i <= 3; i++)
                {
                    string thang = i + quy.Substring(quy.Length - 5, 5);
                    LoadThongKeThang(thang);
                }
            }
            if (timquy == "II")
            {
                for (int i = 4; i <= 6; i++)
                {
                    string thang = i + quy.Substring(quy.Length - 5, 5);
                    LoadThongKeThang(thang);
                }
            }
            if (timquy == "III")
            {
                for (int i = 7; i <= 9; i++)
                {
                    string thang = i + quy.Substring(quy.Length - 5, 5);
                    LoadThongKeThang(thang);
                }
            }
            if (timquy == "IV")
            {
                for (int i = 10; i <= 12; i++)
                {
                    string thang = i + quy.Substring(quy.Length - 5, 5);
                    LoadThongKeThang(thang);
                }
            }
            TinhThuChi();
        }
        public void LayThongKeThang(string thang, ref int thu, ref int chi)
        {
            chi = 0; thu = 0;
            ITKDoanhThuRepository tk = new TKDoanhThuRepository();
            List<string> dsngay = new List<string>();
            IHoaDonRepository hd = new HoaDonRepository();
            IQueryable<HoaDon> list = hd.GetAll();
            foreach (var item in list)
            {
                if (!KiemTraChuoiTrung(dsngay, item.NgayLap) && item.NgayLap.Substring(item.NgayLap.Length - 7, 7) == thang)
                    dsngay.Add(item.NgayLap);
            }
            foreach (string st in dsngay)
            {
                IHoaDonRepository hdn = new HoaDonRepository();
                IQueryable<HoaDon> listhd = hdn.GetByNgay(st);
                if (listhd.Count<HoaDon>() == 0)
                    MessageBox.Show("không tìm thấy");
                else
                {
                    int thungay = 0, chingay = 0;
                    foreach (var item in listhd)
                    {
                        if (item.LoaiHD == true)
                        {
                            chingay = item.TongTien;
                            chi += item.TongTien;
                        }
                        else
                        {
                            thungay = item.TongTien;
                            thu += item.TongTien;
                        }
                    }
                    TKDoanhThu tkdt = new TKDoanhThu(st, chingay, thungay);
                    TKDoanhThu tktk = tk.GetByNgay(st);
                    if (tktk != null)
                    {
                        if (tktk.ThoiGianTK != tkdt.ThoiGianTK)
                            tk.AddNew(tkdt);
                    }
                    else
                        tk.AddNew(tkdt);
                }
            }
        }
        public void LoadThongKeTheoQuy(string quy)
        {
            int thu = 0, chi = 0;
            string timquy = quy.Substring(4, quy.Length - 9);
            if (timquy == "I")
            {
                for (int i = 1; i <= 3; i++)
                {
                    int thuthang = 0, chithang = 0;
                    string thang = i + quy.Substring(quy.Length - 5, 5);
                    LayThongKeThang(thang, ref thuthang, ref chithang);
                    thu += thuthang;
                    chi += chithang;
                }
            }
            if (timquy == "II")
            {
                for (int i = 4; i <= 6; i++)
                {
                    int thuthang = 0, chithang = 0;
                    string thang = i + quy.Substring(quy.Length - 5, 5);
                    LayThongKeThang(thang, ref thuthang, ref chithang);
                    thu += thuthang;
                    chi += chithang;
                }
            }
            if (timquy == "III")
            {
                for (int i = 7; i <= 9; i++)
                {
                    int thuthang = 0, chithang = 0;
                    string thang = i + quy.Substring(quy.Length - 5, 5);
                    LayThongKeThang(thang, ref thuthang, ref chithang);
                    thu += thuthang;
                    chi += chithang;
                }
            }
            if (timquy == "IV")
            {
                for (int i = 10; i <= 12; i++)
                {
                    int thuthang = 0, chithang = 0;
                    string thang = i + quy.Substring(quy.Length - 5, 5);
                    LayThongKeThang(thang, ref thuthang, ref chithang);
                    thu += thuthang;
                    chi += chithang;
                }
            }
            ListViewItem item = new ListViewItem(quy);
            item.SubItems.Add(chi.ToString());
            item.SubItems.Add(thu.ToString());
            item.SubItems.Add((thu - chi).ToString());
            lvThongKeDoanhThu.Items.Add(item);
            TinhThuChi();
        }
        public void ThongKeTheoNam(string nam)
        {
            btnInThongKe.Visible = true;
            btnLuuThongKe.Visible = true;
            lvThongKeDoanhThu.Items.Clear();
            LoadThongKeTheoQuy("Quý I/" + nam);
            LoadThongKeTheoQuy("Quý II/" + nam);
            LoadThongKeTheoQuy("Quý III/" + nam);
            LoadThongKeTheoQuy("Quý IV/" + nam);
        }


        public void TinhThuChi()
        {
            int tongchi = 0, tongthu = 0, loinhuan = 0;
            for (int i = 0; i < lvThongKeDoanhThu.Items.Count; i++)
            {
                tongchi += int.Parse(lvThongKeDoanhThu.Items[i].SubItems[1].Text);
                tongthu += int.Parse(lvThongKeDoanhThu.Items[i].SubItems[2].Text);
            }
            loinhuan = tongthu - tongchi;
            txtTongChi.Text = tongchi.ToString();
            txtTongThu.Text = tongthu.ToString();
            txtLoiNhuan.Text = loinhuan.ToString();
        }
        public void TinhThuChiHangHoa()
        {
            int tongchi = 0, tongthu = 0, loinhuan = 0;
            for (int i = 0; i < lvThongKeHangHoa.Items.Count; i++)
            {
                tongchi += int.Parse(lvThongKeHangHoa.Items[i].SubItems[2].Text) * int.Parse(lvThongKeHangHoa.Items[i].SubItems[4].Text);
                tongthu += int.Parse(lvThongKeHangHoa.Items[i].SubItems[3].Text) * int.Parse(lvThongKeHangHoa.Items[i].SubItems[5].Text);
            }
            loinhuan = tongthu - tongchi;
            txtTongChi.Text = tongchi.ToString();
            txtTongThu.Text = tongthu.ToString();
            txtLoiNhuan.Text = loinhuan.ToString();
        }


        public void ExportlvThongKeDoanhThu(ListView lv, string sheetName, string title)
        {
            int row = 3;
            //Tạo các đối tượng excel
            Microsoft.Office.Interop.Excel.Application oExcel = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbooks oBooks;
            Microsoft.Office.Interop.Excel.Sheets oSheets;
            Microsoft.Office.Interop.Excel.Workbook oBook;
            Microsoft.Office.Interop.Excel.Worksheet oSheet;
            //Tạo mới một Excel WorkBook             
            oExcel.Visible = true;
            oExcel.DisplayAlerts = false;
            oExcel.Application.SheetsInNewWorkbook = 1;
            oBooks = oExcel.Workbooks;
            oBook = (Microsoft.Office.Interop.Excel.Workbook)(oExcel.Workbooks.Add(Type.Missing));
            oSheets = oBook.Worksheets;
            oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oSheets.get_Item(1);
            oSheet.Name = sheetName;
            // Tạo phần đầu nếu muốn           
            Microsoft.Office.Interop.Excel.Range head = oSheet.get_Range("A1", "D1");
            head.MergeCells = true;
            head.Value2 = title;
            head.Font.Bold = true;
            head.Font.Name = "Tahoma";
            head.Font.Size = "18";
            head.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
            // Tạo tiêu đề cột
            Microsoft.Office.Interop.Excel.Range cl1 = oSheet.get_Range("A3", "A3");
            cl1.Value2 = "Thời gian thống kê";
            cl1.ColumnWidth = 30;

            Microsoft.Office.Interop.Excel.Range cl2 = oSheet.get_Range("B3", "B3");
            cl2.Value2 = "Tổng Chi";
            cl2.ColumnWidth = 20.0;

            Microsoft.Office.Interop.Excel.Range cl3 = oSheet.get_Range("C3", "C3");
            cl3.Value2 = "Tổng Thu";
            cl3.ColumnWidth = 20.0;

            Microsoft.Office.Interop.Excel.Range cl4 = oSheet.get_Range("D3", "D3");
            cl4.Value2 = "Lợi nhuận";
            cl4.ColumnWidth = 20.0;





            Microsoft.Office.Interop.Excel.Range rowHead = oSheet.get_Range("A3", "D3");
            rowHead.Font.Bold = true;
            // Kẻ viền
            rowHead.Borders.LineStyle = Microsoft.Office.Interop.Excel.Constants.xlSolid;
            // Thiết lập màu nền
            rowHead.Interior.ColorIndex = 15;
            rowHead.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;

            // xuất các tiêu đề của các cột

            //for (int i = 0; i < lv.Columns.Count; i++)
            //{
            //    oSheet.Cells[1, (i + 1)] = lv.Columns[i].Text;
            //}

  // xuất các item
            for (int col = 0; col < lv.Items.Count; col++)
            {
                row++;
                for (int k = 0; k < lv.Items[col].SubItems.Count; k++)
                {
                    oSheet.Cells[row, (k + 1)] = lv.Items[col].SubItems[k].Text;
      }
            }
        }
        public void ExportlvThongKeHangHoa(ListView lv, string sheetName, string title)
        {
            int row = 3;
            //Tạo các đối tượng excel
            Microsoft.Office.Interop.Excel.Application oExcel = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbooks oBooks;
            Microsoft.Office.Interop.Excel.Sheets oSheets;
            Microsoft.Office.Interop.Excel.Workbook oBook;
            Microsoft.Office.Interop.Excel.Worksheet oSheet;
            //Tạo mới một Excel WorkBook             
            oExcel.Visible = true;
            oExcel.DisplayAlerts = false;
            oExcel.Application.SheetsInNewWorkbook = 1;
            oBooks = oExcel.Workbooks;
            oBook = (Microsoft.Office.Interop.Excel.Workbook)(oExcel.Workbooks.Add(Type.Missing));
            oSheets = oBook.Worksheets;
            oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oSheets.get_Item(1);
            oSheet.Name = sheetName;
            // Tạo phần đầu nếu muốn           
            Microsoft.Office.Interop.Excel.Range head = oSheet.get_Range("A1", "F1");
            head.MergeCells = true;
            head.Value2 = title;
            head.Font.Bold = true;
            head.Font.Name = "Tahoma";
            head.Font.Size = "18";
            head.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
            // Tạo tiêu đề cột
            Microsoft.Office.Interop.Excel.Range cl1 = oSheet.get_Range("A3", "A3");
            cl1.Value2 = "Ma HH";
            cl1.ColumnWidth = 13.5;

            Microsoft.Office.Interop.Excel.Range cl2 = oSheet.get_Range("B3", "B3");
            cl2.Value2 = "Tên Hàng Hóa";
            cl2.ColumnWidth = 50.0;

            Microsoft.Office.Interop.Excel.Range cl3 = oSheet.get_Range("C3", "C3");
            cl3.Value2 = "SL Nhập";
            cl3.ColumnWidth = 15.0;

            Microsoft.Office.Interop.Excel.Range cl4 = oSheet.get_Range("D3", "D3");
            cl4.Value2 = "SL Bán";
            cl4.ColumnWidth = 15.0;

            Microsoft.Office.Interop.Excel.Range cl5 = oSheet.get_Range("E3", "E3");
            cl5.Value2 = "Giá mua";
            cl5.ColumnWidth = 15.0;

            Microsoft.Office.Interop.Excel.Range cl6 = oSheet.get_Range("F3", "F3");
            cl6.Value2 = "Giá Bán";
            cl6.ColumnWidth = 15.0;

            Microsoft.Office.Interop.Excel.Range rowHead = oSheet.get_Range("A3", "F3");
            rowHead.Font.Bold = true;
            // Kẻ viền
            rowHead.Borders.LineStyle = Microsoft.Office.Interop.Excel.Constants.xlSolid;
            // Thiết lập màu nền
            rowHead.Interior.ColorIndex = 15;
            rowHead.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;

            // xuất các tiêu đề của các cột

            //for (int i = 0; i < lv.Columns.Count; i++)
            //{
            //    oSheet.Cells[1, (i + 1)] = lv.Columns[i].Text;
            //}
   // xuất các item
            for (int col = 0; col < lv.Items.Count; col++)
            {
                row++;
                for (int k = 0; k < lv.Items[col].SubItems.Count; k++)
                {
                    oSheet.Cells[row, (k + 1)] = lv.Items[col].SubItems[k].Text;

                }
            }
        }

        private void rbThongKeHangHoa_CheckedChanged(object sender, EventArgs e)
        {
            if (rbThongKeHangHoa.Checked == true)
            {
                lvThongKeDoanhThu.Visible = false;
                lvThongKeHangHoa.Visible = true;
            }
            else
            {
                rbThongKeHangHoa.Visible = true;
                lvThongKeHangHoa.Visible = false;
            }
        }

        private void cbbThongKeTheoQuy_SelectedIndexChanged(object sender, EventArgs e)
        {
            lvThongKeDoanhThu.Items.Clear();
            lvThongKeHangHoa.Items.Clear();
            btnInThongKe.Visible = false;
            btnLuuThongKe.Visible = false;
        }

        private void dtpThongKeTheoNgay_ValueChanged(object sender, EventArgs e)
        {
            lvThongKeDoanhThu.Items.Clear();
            lvThongKeHangHoa.Items.Clear();
            btnInThongKe.Visible = false;
            btnLuuThongKe.Visible = false;
        }

        private void btnLuuThongKe_Click(object sender, EventArgs e)
        {
            if (rbThongKeDoanhThu.Checked == true)
                ExportlvThongKeDoanhThu(lvThongKeDoanhThu, "Thống kê", "Thống kê doanh thu");
            else
                ExportlvThongKeHangHoa(lvThongKeHangHoa, "Thống kê", "Thống kê hàng hóa");
        }
        #endregion
        #region
        /// <summary>
        /// chưa thực hiện được
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void hienThiChiTietHangHoaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (lvThongKeDoanhThu.SelectedItems.Count > 0)
            {

            }
            else
            {
                MessageBox.Show("Bạn chưa chọn hàng để hiển thị");
            }
        }
        public static bool tkthang = false;
        public static bool tktheoquy = false;
        public static string tk = "";
        private void hienThiChiTietNgayToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (lvThongKeDoanhThu.SelectedItems.Count > 0)
            {
                if (cbbThongKe.SelectedIndex == 2)
                {
                    tktheoquy = true;
                }
                tk = lvThongKeDoanhThu.SelectedItems[0].SubItems[0].Text;
                FormChiTietThongKe frm = new FormChiTietThongKe();
                frm.ShowDialog();
            }
            else
            {
                MessageBox.Show("Bạn chưa chọn hàng để hiển thị");
            }
        }
        public static string tkmoi = "";
        private void hienThiChiTietThangToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (lvThongKeDoanhThu.SelectedItems.Count > 0)
            {
                tkthang = true;
                tkmoi = lvThongKeDoanhThu.SelectedItems[0].SubItems[0].Text;
                FormChiTietThongKe frm = new FormChiTietThongKe();
                frm.ShowDialog();
            }
            else
            {
                MessageBox.Show("Bạn chưa chọn hàng để hiển thị");
            }
        }

        private void hienThiChiTietThangToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            if (lvThongKeDoanhThu.SelectedItems.Count > 0)
            {
                tkthang = true;
                tk = lvThongKeDoanhThu.SelectedItems[0].SubItems[0].Text;
                FormChiTietThongKe frm = new FormChiTietThongKe();
                frm.ShowDialog();
            }
            else
            {
                MessageBox.Show("bạn chưa chọn hàng");
            }
        }
        // #endregion
        private void hienthichitietthangtoolstripmenuitem_click(object sender, EventArgs e)
        {
            if (lvThongKeDoanhThu.SelectedItems.Count > 0)
            {
                tkthang = true;
                tk = lvThongKeDoanhThu.SelectedItems[0].SubItems[0].Text;
                FormChiTietThongKe frm = new FormChiTietThongKe();
                frm.ShowDialog();
            }
            else
            {
                MessageBox.Show("bạn chưa chọn hàng để hiển thị");
            }
        }
      #endregion

        private void lblChaoMung_Click(object sender, EventArgs e)
        {

        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        //private void tabQuanLy_Selecting(object sender, TabControlCancelEventArgs e)
        //{
        //    if(tkdn.PhanQuyen==2)
        //        tabQuanLy.
        //}

        

        

     
       
    }
}
   